I have a class
public class A<T>
{
public static string B(T obj)
{
return TransformThisObjectToAString(obj);
}
}
The use of string above is purely exemplary. 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'
You can’t. Generic type identifiers have to be known at compile time.
edit
as of other posts, it appears to be possible by dynamicly generating the method and invoking it – which has dangers of course. See Thomas’ and Dathan’s posts for more inforation.