相关疑难解决方法(0)

使用C#中的反射从字符串中获取属性值

我正在尝试使用我的代码中的Reflection 1示例实现数据转换.

GetSourceValue函数有一个比较各种类型的开关,但我想删除这些类型和属性,并GetSourceValue只使用一个字符串作为参数获取属性的值.我想在字符串中传递一个类和属性并解析属性的值.

这可能吗?

1个 原始博客文章的Web Archive版本

c# reflection

860
推荐指数
12
解决办法
68万
查看次数

C#使用Dynamic关键字通过字符串访问属性而不进行反射

我想写类似以下内容:

//  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"的包以某种方式实现了这种类型的功能. …

c# dynamic dynamic-keyword

12
推荐指数
1
解决办法
4527
查看次数

标签 统计

c# ×2

dynamic ×1

dynamic-keyword ×1

reflection ×1