像 javascript 语言一样访问 c# 类属性将使生活变得更加轻松。
我们如何在 C# 中做到这一点?
例如:
someObject["Property"]="simple string";
Console.WriteLine(someObject["FirstName"]);
最佳答案
下面是如何通过添加几行代码在类中启用类似属性包的功能:
partial class SomeClass
{
private static readonly PropertyDescriptorCollection LogProps = TypeDescriptor.GetProperties(typeof(SomeClass));
public object this[string propertyName]
{
get { return LogProps[propertyName].GetValue(this); }
set { LogProps[propertyName].SetValue(this, value); }
}
}
关于c# - C#类的属性包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16131180/