I want run my code bellow,but it says that I can not use varialbe as type,how can I implement?
- using System;
- using System.Linq;
- using System.Reflection;
-
- namespace Mike.ConsoleApp
- {
- class Program
- {
- static void Main(string[] args)
- {
- #region this is ok
- Personal specifiedPersonal = new Personal();
-
- Employee<Personal> employee = new Employee<Personal>();
- employee.DoSth();
- #endregion
-
- #region this would be error
- Assembly assmebly = typeof(Personal).GetTypeInfo().Assembly;
- Type dynamicType = assmebly.ExportedTypes.Where(x => x.Name == "Personal").FirstOrDefault();
- Employee<dynamicType> dynamicEmployee = new Employee<dynamicType>();
- dynamicEmployee.DoSth();
- #endregion
-
- Console.ReadLine();
- }
- public class Personal
- {
- public string FirstName { get; internal set; }
- public string LastName { get; internal set; }
- }
- public class Employee<TModel>
- {
- public void DoSth()
- {
- Console.WriteLine("Done !!!"); ;
- }
- }
- }
- }