I want to cast a string to a Type. Is use this code:
private static T CastToT<T>(string value)
{
return (T) Convert.ChangeType(value, typeof (T));
}
Type nr5 = CastToT<Type>(typeof(Class1).ToString());
But i got a InvalidCastException.
Invalid cast from ‘System.String’ to ‘System.Type’.
Why can’t I cast the string to a Type?
So if you want to retrieve a Type based on a name, you can simply use this method :
typeName is the fullname of the type you want to retrieve. This method will work if the type you are looking for is in the current assembly or in mscorlib.dll.
For example, based on your example :
And you call that method by giving the FullName of the type: