I've been fighting windmills trying to wrap my head around this problem in C#. The short version is that I have two sets of classes in C#. The first set are objects (which inherit from a base object class) and the second set are parameters for those objects (which inherit a base parameter class). Starting with the parameter classes:
// Object types (parameter classes)
public interface IObjectType<in T> where T : BaseObject
{
public void Foo(BaseObject object);
}
public class ObjectTypeA : IObjectType<ObjectA>
{
public void Foo(ObjectA object) {}
}
public class ObjectTypeA : IObjectType<ObjectB>
{
public void Foo(ObjectB object) {}
}
Function Foo
in this case is used to execute some configuration on a given BaseObject object
depending on its type. This works fine, so far, but to have this work with my objects, I need to have an IObjectType
property in the BaseObject
interface. Below I'll try to illustrate what I'm hoping my object classes would look like.
// Objects
public interface Object
{
public IObjectType<?> type;
public void SetType(IObjectType<?> type);
}
public class ObjectA
{
// type is automatically IObjectType<ObjectA> aka ObjectTypeA
// public void SetType(ObjectTypeA type) {}
}
public class ObjectB
{
// type is automatically IObjectType<ObjectB> aka ObjectTypeB
// public void SetType(ObjectTypeB type) {}
}
After many trials with covariance, contravariance, generic abstract classes and other steps I can't recall or am ashamed to admit, I've run out of options on how to implement a generic IObjectType
property which would automatically cast to the derived type it finds itself in.
At this point I'm not sure if I'm treading down into antipattern territory or if I'm missing some piece of C# functionality - has anyone ever approached the same problem before? Should I just abandon my hopes of finding a solution here and try a different approach?