gpt4 book ai didi

c# - C#中如何访问内部对象的属性

转载 作者:太空宇宙 更新时间:2023-11-03 19:14:40 25 4
gpt4 key购买 nike

我有对象 Item,它里面有数据对象。为了访问数据的属性,我使用以下代码:

Item.Data.PropertyName

在 C# 中是否可以通过以下方式访问数据的属性:

Item.PropertyName

没有将属性复制到“Item”对象?

项目类示例:

class Item{

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

其他类似于数据对象实现的数据对象类

最佳答案

是的,通过在 Item 类中包含 wrapper 属性,它将 返回数据的 PropertyName 类 -

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

这样你就可以写Item.PropertyName

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com