我有对象Item,并且其中有数据对象。为了访问数据的属性,我使用以下代码:

Item.Data.PropertyName


C#中可以通过以下任何方式访问Data的属性:

Item.PropertyName


没有复制属性到“项目”对象?

Item类的示例:

class Item{

    public DataObject Data;
    public AnotherDataObject Data1;
    public AnotherDataObject Data2;
}
class DataObject{
    public int Property1;
    public int Property2;
    .....
}
class DataObject1{.....}
......


其他类似于DataObject实现的DataObjects类

最佳答案

是的,在Item类中具有wrapper属性,该属性将成为return PropertyName of Data类-

public string PropertyName
{
   get
   {
      return this.Data.PropertyName;
   }
   set
   {
      this.Data.PropertyName = value;
   }
}


这样,您可以编写Item.PropertyName

关于c# - 如何在C#中访问内部对象的属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17912112/

10-14 12:13
查看更多