C#开放实例委托协方差和抽象数据

C#开放实例委托协方差和抽象数据,c#,delegates,covariance,C#,Delegates,Covariance,堆栈溢出。我是C语言的新手,但有C++的经验,我坚持了一个想法: 我想将具有抽象属性(不是C#属性,而是变量)的对象作为基类,并将N个派生类作为此类继承: ObjWithProps <-- A <-- B <-- N other classes derived one from another 但是,当处理一系列的问题时,这真的会让人头疼 class CPropWrapper<TPropType, TInstType> where TInstType : ObjW

堆栈溢出。我是C语言的新手,但有C++的经验,我坚持了一个想法:

我想将具有抽象属性(不是C#属性,而是变量)的对象作为基类,并将N个派生类作为此类继承:

ObjWithProps <-- A <-- B <-- N other classes derived one from another
但是,当处理一系列的问题时,这真的会让人头疼

class CPropWrapper<TPropType, TInstType> where TInstType : ObjWithProps
{
  // Property ID here
  // Setter Delegate object here
  // Getter Delegate object here
}
类CPropWrapper,其中TInstType:ObjWithProps
{
//这里的属性ID
//这里是Setter委托对象
//这里是Getter委托对象
}
强制转换太多,类型参数太多,模板太多。。。也许有人知道如何在C#中完成这项任务?关键思想:静态prop list,任何派生类(A、B、C、D)都可以将自己的prop添加到list中,封装和最小化类型规范

提前感谢

UPD1: 伪码

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());
  }
}
类ObjWithProps
{
类CPropertyWrapper
{
公共字符串名称;
公共OpenGetterDelegate Getter;
公共OpenSetterLegate设置器;
}
静态列表抽象道具;
公共CProperty GetPropertyByName(字符串名称)
{
//按名称和名称查找属性
返回PropertyFromType(Getter());
}
}
CProperty是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");
  }
}
A类:ObjWithProps
{
智力价值;
public int getIQ(){返回IQValue;}
public void setIQ(int-iq){IQValue=iq;}
受保护的覆盖寄存器rops()
{
//这个只能叫一次
注册表属性(“IQ”、“getIQ”、“setIQ”);
}
}
B类:A
{
mytype1x;
公共myType1 getX(){return X;}
公共void setX(mytype1x){x=x;}
受保护的覆盖寄存器rops()
{
base.registerProps();
注册表属性(“X”、“getX”、“setX”);
}
}

乍一看,您想从WPF中重新发明。至少,我看不到任何概念上的差异。

您能展示一下描述您想要的东西的伪代码吗?什么是“财产”?它只是一个名字,还是一个有价值的名字?如果是这样,你想如何访问该值?@Dennis,更新帖子如果我没有误解你的问题,你能用一个简单的字典吗?@Mones,什么类将被存储为对象?@JacksonRR在你的例子中,你没有做任何特殊的阐述。。只是价值存储。如果您不必对数据进行任何详细说明,您可以简单地将它们存储在字典中,将键定义为常量。丹尼斯,非常感谢。我不知道这个系统已经存在。然而,我需要在不同的平台上使用它,所以System.Windows听起来不太好。所以是否有机会通过智能代理实现复制WPF PropertySystem,或者这是没有希望的?在我的系统中,难点在于——通过代理将实际的getter/setter与PropertyRapper连接起来。也许你可以给我一个提示?@JacksonRR:我不是Mono开发人员,但在你的例子中,我会看看Mono(甚至是废弃的Moonlight)——也许他们已经实现了DP机制。事实上,这不是WPF特有的。
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());
  }
}
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");
  }
}