我正在尝试使用我的代码中的Reflection 1示例实现数据转换.
该GetSourceValue函数有一个比较各种类型的开关,但我想删除这些类型和属性,并GetSourceValue只使用一个字符串作为参数获取属性的值.我想在字符串中传递一个类和属性并解析属性的值.
这可能吗?
我想写类似以下内容:
// I will pass in a number of "properties" specified as strings that I want modified
string[] properties = new [] { "AllowEdit", "AllowDelete" };
// Casting the component I'm using to a dynamic object of some sort ?
dynamic d = myGridComponent;
// Iterate over the strings and set the properties
foreach(var s in properties)
{
//d.s = true; //
//d[s] = true; // this format would be ideal
}
Run Code Online (Sandbox Code Playgroud)
我在想,如果有一个简单的方法来做到这一点,而无需使用反射[ .GetProperty(...).GetValue(...,...)使用新的C#4.0关键字]: dynamic.
似乎可能有某种方式,......我只是不确定确切的机制,并且无法找到合适的资源来将所有部分组合在一起.
想法?
[编辑]看起来有一个名为"Clay"的包以某种方式实现了这种类型的功能. …