C# 动态调用泛型方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hanjun0612/article/details/84954340
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;
            }
        }

猜你喜欢

转载自blog.csdn.net/hanjun0612/article/details/84954340