【发布时间】:2011-02-16 12:44:28
【问题描述】:
我想写一些类似下面的东西:
// 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
}
我想知道是否有一种简单的方法可以在不使用反射 [.GetProperty(...).GetValue(...,...)] 的情况下使用新的 C# 4.0 关键字:dynamic。
似乎有某种方式,......我只是不确定确切的机制,并且无法找到合适的资源来将所有部分放在一起。
想法?
[编辑]
看起来有一个名为“Clay”的包以某种方式实现了这种类型的功能。
Clay on CodePlex
Scott Hanselman on the Subject
【问题讨论】:
标签: c# dynamic dynamic-keyword