C# 动态调用泛型方法

时间:2022-08-30 20:22:42
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 
版权声明:本文为博主原创文章,转载请附上博文链接!