JacksonRR
JacksonRR

Reputation: 227

C# Open Instance Delegate covariance and abstract data

stackoverflow. I'm new to C#, but have experience in C++ and I got stuck with one idea realization:

I want to make an object with abstract properties(not C# properties, but variables) as a base class and N derived classes with such inheritance:

ObjWithProps <-- A <-- B <-- N other classes derived one from another

Properties list is static, so it will be initialized once per type, not per object. Each of A and B can add own abstract properties with unique string-represented names. First of all I was thinking of making it with OpenInstanceDelegates, but it turns out, that delegates can't be covariant, am I right ?

public delegate T OpenGetterDlg<T>(ObjWithProps instance);

I can't simply bind function A.GetSomething() to OpenGetterDlg because of different this parameter and covariance doesn't works here.

I could do that instead:

public delegate TPropType OpenGetterDlg<TPropType, TThisType>(TThisTypeinstance);

but it becomes real pain in the ass when dealing with a list of

class CPropWrapper<TPropType, TInstType> where TInstType : ObjWithProps
{
  // Property ID here
  // Setter Delegate object here
  // Getter Delegate object here
}

Too many casts, too many type params, too many templates ... Maybe someone knows how do that task in C# ? The key ideas: static prop list, any derived classes (A, B, C, D) can add their own props to list, encapsulated and minimal type specification.

Thanks in advance!

UPD1: Pseudocode

class ObjWithProps
{
  class CPropertyWrapper
  {
     public string Name;
     public OpenGetterDelegate Getter;
     public OpenSetterDelegate Setter;
  }

  static List<CpropertyWrapper> AbstractProps;

  public CProperty GetPropertyByName(string name)
  {
     // Find property by name and
     return PropertyFromType(Getter());
  }
}

CProperty is a base wrapper class for types like int, float, myType1, myType2.

class A: ObjWithProps
{
  int IQValue;

  public int getIQ() { return IQValue; }
  public void setIQ(int iq) { IQValue = iq; }

  protected override registerProps()
  {
    // this one should be called only once
    registerProperty<int>("IQ", "getIQ", "setIQ");
  }
}

class B: A
{
  myType1 X;

  public myType1 getX() { return X; }
  public void setX(myType1 x) { X= x; }

  protected override registerProps()
  {
    base.registerProps();
    registerProperty<myType1>("X", "getX", "setX");
  }
}

Upvotes: 0

Views: 183

Answers (1)

Dennis
Dennis

Reputation: 37780

At first look, you want to re-invent dependency properties from WPF. At least, I can't see any conceptual differences.

Upvotes: 1

Related Questions