-1

I have class name in string. Example

string nameClass = "MyClass";

I need convert this string to TYPE. Example

List<here i need get class type from string> ListMyClass = new List<>....

I have reference so when i write List it is correct but i need dynamicaly change Type of List (i need it for method but principle is same)

But i dont need create Instance of List. I have method exampel Write(type t) and i need change TYPE from string value.

I try write what i need exactly.

I have EventAggregator. EA subsribe method according to type..so when i publish(string) a i need subsribe Method(metoda)...I have many class for Publish and i need dynamicaly change subsribe method type..i have type in xml file, so i get name of class in string "Class1" and i need told to subsribe that type is Class1....sorry for my english.

  • You should really google it. It isn't a tricky issue. Just follow up this one: http://stackoverflow.com/questions/11107536/convert-string-to-type-c-sharp – Shmwel Jul 16 '15 at 06:23
  • Hello, thx for answer. But i dont want create Instance of List. It was only example....I have method as write(type t) and i need dynamically chnange type from string, but if i have class and do GetType() I cant replace TYPE compilator write Type is variable but used like type...I need change TYPE in Write method according to string value – Tomáš Vodsloň Jul 16 '15 at 06:44
  • It's not achievable what you want to do. The compiler must know from the beginning the type. – Shmwel Jul 16 '15 at 06:46
  • I have EventAggregator. EA subsribe method according to type..so when i publish(string) a i need subsribe Method(metoda)...I have many class for Publish and i need dynamicaly change subsribe method type..i have type in xml file, so i get name of class in string "Class1" and i need told to subsribe that type is Class1....sorry for my english. – Tomáš Vodsloň Jul 16 '15 at 06:56
  • @SJD: you can do everything at runtime. You can build generic types and call generic methods with reflection. You can even create new classes at runtime with emit. In many cases, this is very questionable, because you do not need generics when you have runtime types. But sometimes it makes sense. – Stefan Steinegger Jul 16 '15 at 07:06
  • @StefanSteinegger But I don't know why you would do that? I mean, there are another ways of doing it (more elegant) -- instead of specifying the type via a string. :( – Shmwel Jul 16 '15 at 07:17
  • @SJD: Sure, I agree. I just responded to you statement that "The compiler must know from the beginning the type". The problem with the question here is that I do not understand what the OP actually wants to do. – Stefan Steinegger Jul 16 '15 at 07:27
  • @StefanSteinegger I edited first question on example with EventAggregator – Tomáš Vodsloň Jul 16 '15 at 08:02

1 Answers1

-1

You can use Type.GetType(string) to get the Type object associated with a type by its name. Then use Activator.CreateInstance to creates an instance of the specified

string nameClass = "MyClass";
Type elementType = Type.GetType(nameClass);
Type listType = typeof(List<>).MakeGenericType(new Type[] { elementType });
dynamic ListMyClass = Activator.CreateInstance(listType);
Liem Do
  • 933
  • 1
  • 7
  • 13
  • Hello, thx for answer. But i dont want create Instance of List. It was only example....I have method as write(type t) and i need dynamically chnange type from string, but if i have class and do GetType() I cant replace TYPE compilator write Type is variable but used like type...I need change TYPE in Write method according to string value. – Tomáš Vodsloň Jul 16 '15 at 06:44
  • Did you try with `dynamic`? – Liem Do Jul 16 '15 at 06:54
  • i dont know how can use dynamic in this case? – Tomáš Vodsloň Jul 16 '15 at 06:58
  • The dynamic type enables the operations in which it occurs to bypass compile-time type checking. You won't get compile error. https://msdn.microsoft.com/en-us/library/dd264741.aspx – Liem Do Jul 16 '15 at 07:10