Accessing Static Methods on a Generic class in c#
- by mrlane
Hello, I have the following situation in code, which I suspect may be a bit dodgey:
I have a class:
abstract class DataAccessBase<T> : IDataAccess where T : AnotherAbstractClass
This class DataAccessBase also has a static factory method which creates instances of derived classes of itself using an enum value in a which statement to decide which derived type to create:
static IDataAccess CreateInstance(TypeToCreateEnum)
Now, the types derived from DataAccessBase<T> are themselves NOT generic, they specify a type for T:
class PoLcZoneData : DataAccessBase<PoLcZone> // PoLcZone is derived from AnotherAbstractClass
So far I am not sure if this is pushing the limits of good use of generics, but what I am really concerned about is how to access the static CreateInstance() method in the first place:
The way I am doing this at the moment is to simply pass any type T where T : AnotherAbstractClass. In particular I am passing AnotherAbstractClass itself. This allows compilation just fine, but it does seem to me that passing any type to a generic class just to get at the statics is a bit dodgey.
I have actually simplified the situation somewhat as DataAccessBase<T> is the lower level in the inheritance chain, but the static factory methods exists in a middle tier with classes such as PoLcZoneData being the most derived on the only level that is not generic.
What are peoples thoughts on this arrangement?