Calling a Static Method on a Generic Type Parameter

Calling a static method on a generic type parameter

In this case you should just call the static method on the constrainted type directly. C# (and the CLR) do not support virtual static methods. So:

T.StaticMethodOnSomeBaseClassThatReturnsCollection

...can be no different than:

SomeBaseClass.StaticMethodOnSomeBaseClassThatReturnsCollection

Going through the generic type parameter is an unneeded indirection and hence not supported.

How do I call a static method on a generic class without specifying a type?

You can make a separate class with the same name but no type parameters.

Note that the type parameters are part of the identity of the class. If you're trying to clear all fields in all type-parameterized versions of your class, that isn't directly possible.

Calling a static method using generic type

No you cannot do it if A is a generic type. (Bozho answered to fast :) and probably thought A was concrete type.

What will work is the following.

abstract class Agent extends Blah<ConcreteA>{
void callAgent();
Agent() {
ConcreteA.add();
}
}

but it's probably not what you want to do.

After reading your comments it sounds like what you really want to do is:

abstract class Agent<A extends SomeClassThatSupportsAdd> {

void callAgent();
protected abstract A createNew();

Agent() {
A a = createNew();
A.add();
}
}

Your subclasses will have to override createNew().

If you still do not like that you can take a look at AspectJ which will allow you to do some constructor magic (see how spring does @Configurable) but that gets far trickier and complicates things.

Another option is Scala. Java does not do inheritance on static methods so you can't get parameterized modules (groups of functions in some languages this is called a functor ... ocaml). However Scala supports a singleton "object" that does allow for parametric functional polymorphic inheritance.

Static method in a generic class?

You can't use a class's generic type parameters in static methods or static fields. The class's type parameters are only in scope for instance methods and instance fields. For static fields and static methods, they are shared among all instances of the class, even instances of different type parameters, so obviously they cannot depend on a particular type parameter.

It doesn't seem like your problem should require using the class's type parameter. If you describe what you are trying to do in more detail, maybe we can help you find a better way to do it.

How to access static methods of generic types

The reason you can't reference the static member like this:

O.MyStaticMethod(); 

Is because you don't know what type O is. Yes, it inherits from BusinessObject, but static members are not inherited between types, so you can only reference MyStaticMethod from BusinessObject.

Why do I have to use T on static method call on a generic class

Because you haven't created a class called GenericClass, you've created an open generic class called GenericClass<T>.

There is nothing to stop you from also creating a class GenericClass in the same namespace, and nothing to stop you from creating also a class called GenericClass<T1,T2>. All of these could exist in the same namespace and have no explicit or implicit relationship between them, unless you declare one.

So, if you want to invoke a static method on "the GenericClass class that is generic in one type parameter", you've got to say that somehow, and you've found how to do it - by supplying a type parameter.

One could argue that, if the static method doesn't use the type parameter, it's redundant - so why can you not call it just by using the still-open type parameter? Well, firstly because that would have to be new syntax to allow that to happen1. And second, what happens if your method accesses any static fields? With generic types, each unique type used as the type parameter causes a new set of static fields to exist.


why isnt the syntax for static methods genericClass<T>.StaticMethod(); for example as ...

The above was added after my initial answer and I'd hoped this was already addressed in footnote 1 below. But in case it's not clear, this simple syntax won't work. You need to invent some new syntax because you might have:

class Abc<T> {
void DoSomething(){
GenericClass<T>.StaticMethod();
}
}

or

namespace X {
class T {
}
class Abc {
void DoSomething(){
GenericClass<T>.StaticMethod();
}
}
}

In both of the above examples T is already defined by the outer scope. So you need some other way of saying "I don't want to supply a type for the first type parameter of this generic".


1E.g. being able to say GenericClass<T>.Showcase. Any new syntax to allow it couldn't be so straightforward. Because a) there may be a generic type parameter T in scope in the calling context, or b) the name of the generic type parameter may clash with some other type name that is in scope in the calling context.



Related Topics



Leave a reply



Submit