Click to See Complete Forum and Search --> : Type parameter question


NetMaster
September 19th, 2008, 04:23 AM
Hi,

I've defined a generic class where I tried to access a static method in
'T', but I got the error message: "'T' is a 'type parameter', which is not valid in the given context"


public class MyGenericClass<T>
where T : new()
{
public void MyMethod()
{
string s = T.GetString(); // GetString() is a static method
^
error here

}
}
Is there something missing in the constraints or isn't it possible to access static methods in the type parameter ?
PS: I'm using MVS 2005

boudino
September 19th, 2008, 07:03 AM
Generics don't work this way. Moreover, it seems to me like bad design. Maybe, you want generic static method?

public static class StringFactory
{
public static string GetString<T>()
{
// determine return value on basis of T
}
}

NetMaster
September 19th, 2008, 07:24 AM
Maybe it is bad design. I'll have to solve the problem in some other way.
Thanks for the help.