Lui Wang

Lui Wang

  • NA
  • 5
  • 942

How to pass vairable to class ?

Dec 17 2018 12:04 AM
 I want run my code bellow,but it says that I can not use varialbe as type,how can I implement? 
  1. using System;    
  2. using System.Linq;    
  3. using System.Reflection;    
  4.     
  5. namespace Mike.ConsoleApp    
  6. {    
  7.     class Program    
  8.     {    
  9.         static void Main(string[] args)    
  10.         {    
  11.             #region this is ok    
  12.             Personal specifiedPersonal = new Personal();    
  13.     
  14.             Employee<Personal> employee = new Employee<Personal>();    
  15.             employee.DoSth();    
  16.             #endregion    
  17.   
  18.             #region this would be error    
  19.             Assembly assmebly = typeof(Personal).GetTypeInfo().Assembly;    
  20.             Type dynamicType = assmebly.ExportedTypes.Where(x => x.Name == "Personal").FirstOrDefault();    
  21.             Employee<dynamicType> dynamicEmployee = new Employee<dynamicType>();    
  22.             dynamicEmployee.DoSth();    
  23.             #endregion               
  24.     
  25.             Console.ReadLine();    
  26.         }    
  27.         public class Personal    
  28.         {    
  29.             public string FirstName { getinternal set; }    
  30.             public string LastName { getinternal set; }    
  31.         }    
  32.         public class Employee<TModel>    
  33.         {    
  34.             public void DoSth()    
  35.             {    
  36.                 Console.WriteLine("Done !!!"); ;    
  37.             }    
  38.         }    
  39.     }        
  40. }    
 

Answers (2)