No you can't do that. There are a number of problems with what you're trying to do:
- You cannot use static types as type arguments—e.g.
List<A>
. - You cannot use static types as method parameters—e.g.
AddElement(A element)
. - You cannot make a static type abstract, since there's no way to inherit from it.
- You cannot make a static method abstract even in a non-static class, since it cannot be overridden.
From how you've described the problem, I can see no need for static types or static methods here. Just create a simple abstract class and inherit from it:
public abstract class A {
public abstract void F();
}
public class B : A {
public override void F() {
...
}
}
public class Storage {
private List<A> list;
public void AddElement(A element) {
list.Add(element);
}
public void DoStuff() {
foreach(A element in list)
element.F();
}
}
Alternatively, you can create a List<Type>
and use reflection to invoke static methods on those types. For example:
public static class A {
public static void F() { ... }
}
public static class B {
public static void F() { ... }
}
List<Type> typeList = new List<Type> { typeof(A), typeof(B) };
foreach(var type in typeList)
{
type.GetMethod("F").Invoke(null, null);
}
But using reflection is going to be slower than using direct method calls, and you'll loose all type-safety with this method (unless you write the type-checks yourself).