public class A<T>
{
public static void B()
{
}
}
How I can call method B like this:
Type C = typeof(SomeClass);
A<C>.B()
You need to use reflection. MakeGenericType allows you to get the Type with specific generic arguments and then you can get and call any method on it as you like.
void Main()
{
Type t = typeof(int);
Type at = typeof(A<>).MakeGenericType(t);
at.GetMethod("B").Invoke(null, new object[]{"test"});
}
public class A<T>
{
public static void B(string s)
{
Console.WriteLine(s+" "+typeof(T).Name);
}
}
As a performance optimization you could use reflection to get a delegate for each type which you then can invoke without further reflection.
Type t = typeof(int);
Type at = typeof(A<>).MakeGenericType(t);
Action<string> action = (Action<string>)Delegate.CreateDelegate(typeof(Action<string>), at.GetMethod("B"));
action("test");
A<SomeClass>.B();?Cis a variable, not a type.