Calling a Static Method on a Class Type passed as a Generics Argument.

E

ESPNSTI

Hi,

I'm trying to call a static method on a class type that is passed into a
method through generics, for example:

public bool Test<TestStaticType>() where TestStaticType : TestStaticBase,
new()
{
return TestStaticType.Foo(); // <-- The error is produced here
}

It's giving me the following error:
'TestStaticType' is a 'type parameter', which is not valid in the given
context

Why doesn't that work?

FYI, I'm using VS2005 Beta 2.

Thanks,
Erik
________________________________________

using System;
using System.Collections.Generic;
using System.Text;

namespace Test
{
public class TestStaticBase
{
public static bool Foo()
{
return true;
}
}

public class TestStatic : TestStaticBase
{
}

public class Class1
{
public bool Test<TestStaticType>()
where TestStaticType: TestStaticBase, new()
{
return TestStaticType.Foo(); // <-- The error is produced here
}

public void main()
{
bool test = Test<TestStatic>();
}
}
}
 
M

Marcus Andrén

Hi,

I'm trying to call a static method on a class type that is passed into a
method through generics, for example:

public bool Test<TestStaticType>() where TestStaticType : TestStaticBase,
new()
{
return TestStaticType.Foo(); // <-- The error is produced here
}

From the CSharpFinalWorkingDraftApril2005.pdf:

* A type parameter cannot be used in a member access or type name to
identify a static member or a nested type (§10.8, §14.5.4).

Which basically means that you can't use static methods on generic
types.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top