zoukankan      html  css  js  c++  java
  • C# 动态调用泛型方法

    static void Main(string[] args)
            {
                #region 具体类型可传递。
                Personal specifiedPersonal = new Personal();
     
                Employee<Personal> employee = new Employee<Personal>();
                employee.Create(specifiedPersonal);
                #endregion
     
     
                #region 动态类型传递到TModel。
                //假设传递一个a进来
                Personal a = new Personal();
                var type = typeof(Employee<>).MakeGenericType(a.GetType());
                dynamic a_Context = Activator.CreateInstance(type);
                var q =  a_Context.Create(a);
                Console.WriteLine(q);
     
                #endregion
     
                Console.ReadLine();
            }
     
            public class Personal
            {
                public string FirstName { get; internal set; }
                public string LastName { get; internal set; }
            }
            public interface IEmployee<TModel>
            {
                Guid Create(TModel model);
                bool Update(TModel model);
            }
     
            public class Employee<TModel> : IEmployee<TModel>
            {
                public Guid Create(TModel model)
                {
                    // TODO :
                    return Guid.NewGuid();
                }
                public bool Update(TModel model)
                {
                    // TODO :
                    return true;
                }
            }
    --------------------- 
    作者:正怒月神 
    来源:CSDN 
    原文:https://blog.csdn.net/hanjun0612/article/details/84954340 
    版权声明:本文为博主原创文章,转载请附上博文链接!
  • 相关阅读:
    @Controller 与 @RestController 的区别
    Java泛型
    Java面试被经常问到的常用算法
    jdk和jre的区别
    Spring获取对象的方式
    xsi:schemaLocation的作用
    SpringBoot学习(一)
    docker-elk装IK自定义分词库
    MySQL存储引擎
    docker环境下elasticsearch安装ik和拼音分词
  • 原文地址:https://www.cnblogs.com/hanjun0612/p/10101938.html
Copyright © 2011-2022 走看看