Advertisement
elena1234

Reflection - create instance

May 28th, 2021 (edited)
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.71 KB | None | 0 0
  1.            var instance = (Person) Activator.CreateInstance(typeof(Person), "Pesho");
  2.  
  3. //
  4.             Type type = Type.GetType("Reflection.Demo.Ferrari");
  5.             var obj = Activator.CreateInstance(type, new object[]{ }) as Ferrari; // create instance with reflection
  6.             Console.WriteLine(obj.Name);
  7.  
  8. //
  9.             string make = Console.ReadLine();
  10.             Type type = Type.GetType($"Reflection.Demo.{make}");
  11.             var obj = Activator.CreateInstance(type) as Car; // create instance with reflection
  12.             Console.WriteLine(obj.Name);
  13.  
  14. //
  15.  
  16.             Type type = Type.GetType($"System.Text.StringBuilder");
  17.             StringBuilder sbInstCapacity = (StringBuilder)Activator.CreateInstance(type, new object[] { 10 });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement