物业袋C#类 [英] Property bag for C# class
本文介绍了物业袋C#类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
访问C#类的属性,如JavaScript语言将使生活轻松了许多。
我们如何能在C#呢?
例如:
someObject [属性] =简单的字符串;
Console.WriteLine(someObject [名字]);
解决方案
下面是如何启用财产的袋状在你的类功能,通过添加几行代码:
部分类SomeClass的
{
私有静态只读PropertyDescriptorCollection LogProps = TypeDescriptor.GetProperties(typeof运算(SomeClass的));
公共对象本[字符串参数propertyName]
{
{返回LogProps [propertyName的] .GetValue(本); }
集合{LogProps [propertyName的] .SetValue(这一点,值); }
}
}
Accessing c# class properties like javascript language would make life a lot easier.
How we can do it in C#?
For example:
someObject["Property"]="simple string";
Console.WriteLine(someObject["FirstName"]);
解决方案
Here is how you can enable property-bag-like functionality in your classes by adding a few lines of code:
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#类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文