Skip to main content
Copy edited (e.g. ref. <http://english.stackexchange.com/questions/4645/is-it-ever-correct-to-have-a-space-before-a-question-or-exclamation-mark#comment206109_4645>).
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

How can I get generic Type from a string representation?

I have MyClass<T>.

And then I have this string s = "MyClass<AnotherClass>";
How. How can I get Type from the string s s?

One way (ugly) is to parse out the "<" and ">" and do:

Type acType = Type.GetType("AnotherClass"); Type whatIwant = typeof (MyClass<>).MakeGenericType(acType); 

But is there a cleaner way to get the final type without any parsing, etc.  ? Thanks.

How can I get generic Type from string representation

I have MyClass<T>.

And then I have this string s = "MyClass<AnotherClass>";
How can I get Type from the string s ?

One way (ugly) is to parse out the "<" and ">" and do:

Type acType = Type.GetType("AnotherClass"); Type whatIwant = typeof (MyClass<>).MakeGenericType(acType); 

But is there a cleaner way to get the final type without any parsing etc.  ? Thanks.

How can I get generic Type from a string representation?

I have MyClass<T>.

And then I have this string s = "MyClass<AnotherClass>";. How can I get Type from the string s?

One way (ugly) is to parse out the "<" and ">" and do:

Type acType = Type.GetType("AnotherClass"); Type whatIwant = typeof (MyClass<>).MakeGenericType(acType); 

But is there a cleaner way to get the final type without any parsing, etc.?

this question is generic specific. having that in title helps.
Link
nawfal
  • 73.8k
  • 59
  • 342
  • 379

C# How can I get generic Type from a string representation

added 14 characters in body
Source Link
Joel Coehoorn
  • 418.4k
  • 114
  • 582
  • 820

I have MyClassMyClass<T>.

And then I have this string s = "MyClass";string s = "MyClass<AnotherClass>";
How can I get Type from the string s ?

One way (ugly) is to parse out the "<" and ">" and do:
Type acType = Type.GetType("AnotherClass");
Type whatIwant = typeof (MyClass<>).MakeGenericType(acType);

Type acType = Type.GetType("AnotherClass"); Type whatIwant = typeof (MyClass<>).MakeGenericType(acType); 

But is there a cleaner way to get the final type without any parsing etc. ? Thanks.

I have MyClass.

And then I have this string s = "MyClass";
How can I get Type from the string s ?

One way (ugly) is to parse out the "<" and ">" and do:
Type acType = Type.GetType("AnotherClass");
Type whatIwant = typeof (MyClass<>).MakeGenericType(acType);

But is there a cleaner way to get the final type without any parsing etc. ? Thanks.

I have MyClass<T>.

And then I have this string s = "MyClass<AnotherClass>";
How can I get Type from the string s ?

One way (ugly) is to parse out the "<" and ">" and do:

Type acType = Type.GetType("AnotherClass"); Type whatIwant = typeof (MyClass<>).MakeGenericType(acType); 

But is there a cleaner way to get the final type without any parsing etc. ? Thanks.

Source Link
DeeStackOverflow
  • 2.8k
  • 5
  • 23
  • 23
Loading