Generics calling static methods?

T

taub.adam

Hi,

Can generics be used to call a static method of a class? The below
example gives an error in my generictest class. Anybody know why?

class foo
{
static public void Test() { Console.WriteLine("foo.Test");}
}

class GenericTest<T>
{
public void CallTest() { T.Test();}
}
 
J

Jon Skeet [C# MVP]

Can generics be used to call a static method of a class?

No - because there's no constraint which can be used to indicate that a
given static method must be available on a type parameter, so the
compiler can't know whether or not the method will be there.

(It's possible that you could have a constraint of T : SomeBaseType
where SomeBaseType defines the method, but in that case you should just
call SomeBaseType.SomeMethod anyway.)
The below
example gives an error in my generictest class. Anybody know why?

What would you expect to happen if someone had called new
GenericTest<string>().CallTest()?

Jon
 
V

Vadym Stetsyak

Hello, (e-mail address removed)!

ta> Can generics be used to call a static method of a class?

AFAIK no, because generics use instance of specified type( in your case T )

If you want to use specific methods you can define a constraint.
public interface IFoo
{
void Method();
}

public class Foo : IFoo
{
public Foo()
{
}

public void Method()
{
Console.WriteLine("test");
}
}

public class GenericTest<T> where T : IFoo, new()
{
public void CallTest()
{
T t = new T();
t.Method();
}
}

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Hi,

Can generics be used to call a static method of a class? The below
example gives an error in my generictest class. Anybody know why?

Remember that you need to use the type to invoke a static method. Generic is
quite the opposite, you do not especify the type until runtime.

Even if the static method is member of the generic constrain you cannot use
it, as you can use a derived type to instantiate and the derived type does
not implement the static method (the base does) .

The only way to do this (in case that you really need to) is using
reflection, you would have to deal with the case that the static method is
not present though.


What are you trying to do?
 
T

taub.adam

Thanks all for the help!

I have some code that is repeated in a few places and can pretty nicely
be changed to a generic method except for this one call to a different
static method based on the caller.

In my case the simplest solution will be to pass in a delegate as a
parameter (I think :) ). I just thought it may be possible to use the
generic type instead.

Reflection would work too but its a bit heavy for this case.

In answer to what I would expect at runtime for a "bad case". I had
thought the evaluation of the type was done at runtime and therefore
expected a runtime exception if I passed a type that didnt fit my
usage.

-Adam
 

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