C#动态添加属性

class DynamicInputParams: DynamicObject
{
Dictionary<string, object> property = new Dictionary<string, object>(); public override bool TryGetMember(GetMemberBinder binder, out object result)
{
string name = binder.Name;
return property.TryGetValue(name, out result);
} public override bool TrySetMember(SetMemberBinder binder, object value)
{
property[binder.Name] = value;
return true;
}
}

首先继承DynamicObject,添加property属性,并实现下面的两个方法,然后调用方法如下:

dynamic  dyInputParams = new DynamicInputParams();

            dyInputParams.A = "abc";
dyInputParams.B = "d"; var s = dyInputParams.A;

s的值为“abc”

但是该属性没有get/set方法,所以在PropertyGrid控件中无法显示该属性。

上一篇:linux如何启动/停止/重启MySQL


下一篇:Less函数说明