0

Can I do this in C#:

string str="string";
Type typ=typeof(str);

Can any one help me to create generic type converter without do multiple conditions

Islam Ibrahim
  • 85
  • 1
  • 9
  • `str.GetType()` or `typeof(string)` – King King Aug 22 '13 at 08:28
  • Regarding type conversions, have a look at [my answer on another question](http://stackoverflow.com/questions/18360322/is-using-successive-tryparse-calls-a-sound-way-to-guess-a-strings-real-type/18362420#18362420) – Sameer Singh Aug 22 '13 at 08:31

1 Answers1

5

The closest you will be able to get is by using Type.GetType(System.String).

This requires using the assembly qualified name, unless the type is defined in mscorlib, where you will be able to use just the namespace qualified name.

e.g for a class from mscorlib:

Type stringType = Type.GetType("System.String");

or for a class not from mscorlib:

Type dependencyPropertyType = Type.GetType("System.Windows.DependencyProperty, WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
Lukazoid
  • 19,016
  • 3
  • 62
  • 85