I just went through a handful of SO questions on this topic and found out that this is (currently?) not possible to define static members in interfaces or make static methods virtual. I'm now struggling with a situation. Let me try to depict it with a simple example:
I have an interface named say IAnimal
with the following definition:
interface IAnimal {
...
int Chromosomes {get; } //should ideally be static, but that's not allowed!
...
}
Then I have a base class Animal that implements IAnimal
and provides implementation of some methods common to all animals:
abstract class Animal : IAnimal {
...
public abstract int Chromosomes {get; } //necessary becuz of IAnimal
...
}
Below this I then have Dog
, Cat
and Zebra
classes that inherit from Animal
and provide concrete implementations of this property for their species.
Now the problem: These classes are discovered at runtime (through Assembly.LoadFile()
becuz of the pluggable-modules). The discovered classes are kept in a List<System.Type>
that we later use to create concrete dogs and cats. The input parameter for the creation of an animal is chromosomes number, so for example they'll ask: Create an animal of the type that has 72 chromosomes. So I need to somehow get the Chromosome property's value of each Type
in the List
and return the first matching Type
.
Since Chromosomes
is not static (if it were, I could easily run a static member of a given Type), am I forced to create a temporary object of each type and then call Chromosomes
with it, or is there a better way? All the elements in my List
are guarenteed to be IAnimal
if that is of help.