Setting generic type at runtime
Posted
by destroyer of evil
on Stack Overflow
See other posts from Stack Overflow
or by destroyer of evil
Published on 2010-04-09T02:24:36Z
Indexed on
2010/04/09
2:33 UTC
Read the original article
Hit count: 321
I have a class
public class A<T>
{
public static string B(T obj)
{
return TransformThisObjectToAString(obj);
}
}
I can call the static function like this just fine on a known/specified type:
string s= A<KnownType>.B(objectOfKnownType);
How do I make this call, if I don't know T beforehand, rather I have a variable of type Type that holds the type. If I do this:
Type t= typeof(string);
string s= A<t>.B(someStringObject);
I get this compiler error:
Cannot implicitly convert type 't' to 'object'
© Stack Overflow or respective owner