|
-
September 19th, 2008, 04:23 AM
#1
Type parameter question
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"
Code:
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
Last edited by NetMaster; September 19th, 2008 at 05:05 AM.
-
September 19th, 2008, 07:03 AM
#2
Re: Type parameter question
Generics don't work this way. Moreover, it seems to me like bad design. Maybe, you want generic static method?
Code:
public static class StringFactory
{
public static string GetString<T>()
{
// determine return value on basis of T
}
}
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
September 19th, 2008, 07:24 AM
#3
Re: Type parameter question
Maybe it is bad design. I'll have to solve the problem in some other way.
Thanks for the help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|