Calling a static method on a generic type parameter

Posted by Remi Despres-Smyth on Stack Overflow See other posts from Stack Overflow or by Remi Despres-Smyth
Published on 2008-10-13T03:49:43Z Indexed on 2010/06/16 6:52 UTC
Read the original article Hit count: 448

Filed under:
|

I was hoping to do something like this, but it appears to be illegal in C#:


public Collection MethodThatFetchesSomething<T>()
    where T : SomeBaseClass
{
    return T.StaticMethodOnSomeBaseClassThatReturnsCollection();
}

I get a compile-time error: "'T' is a 'type parameter', which is not valid in the given context."

Given a generic type parameter, how can I call a static method on the generic class? The static method has to be available, given the constraint.

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics