C# Silverlight中匿名类型属性的反射失败
我在VS 2010中使用Silverlight 4,并尝试对匿名类型进行反射,但我尝试通过方法“…”访问方法“…”失败。。我为此尝试了各种解决方法,但找不到简单的方法 class.CallAnonymousSimpleClass,HelloFunc,new{strIn=Boo}C# Silverlight中匿名类型属性的反射失败,c#,silverlight,reflection,anonymous-types,propertyinfo,C#,Silverlight,Reflection,Anonymous Types,Propertyinfo,我在VS 2010中使用Silverlight 4,并尝试对匿名类型进行反射,但我尝试通过方法“…”访问方法“…”失败。。我为此尝试了各种解决方法,但找不到简单的方法 class.CallAnonymousSimpleClass,HelloFunc,new{strIn=Boo} public void CallAnonymous(string cName, string cAction, object anonymousParms) { Type anonymo
public void CallAnonymous(string cName, string cAction, object anonymousParms)
{
Type anonymousType = anonymousParms.GetType();
PropertyInfo[] props = anonymousType.GetProperties();
ServiceParam serviceParam = new ServiceParam();
foreach (var info in props)
{
string propertyName = info.Name;
object propertyObj = info.GetValue(anonymousParms, null);
// Throw the exception on PropertyInfo.GetValue()
serviceParam.Add(propertyName, propertyObj);
}
}
[编辑]
通过在项目中应用[assembly:InternalsVisibleToSystem.Windows]程序集级属性,实际上可以绑定到匿名类型。这将使Silverlight的数据绑定系统能够查看编译器生成的内部类型
不幸的是,您无法访问匿名对象属性,因为编译器将它们标记为内部,并且Silverlight安全沙箱阻止您访问内部成员
当前可以做的是调用匿名对象ToString方法并从字符串表示中提取值
希望这有帮助。[编辑]
通过在项目中应用[assembly:InternalsVisibleToSystem.Windows]程序集级属性,实际上可以绑定到匿名类型。这将使Silverlight的数据绑定系统能够查看编译器生成的内部类型
不幸的是,您无法访问匿名对象属性,因为编译器将它们标记为内部,并且Silverlight安全沙箱阻止您访问内部成员
当前可以做的是调用匿名对象ToString方法并从字符串表示中提取值
希望这有帮助。我发现一篇很好的文章解决了我的问题。本文解释了为什么在评估从不同程序集的公共方法返回的匿名类型实例时,C4.0的动态特性似乎不起作用。感谢ligaz给了我们一个好的起点
我发现一篇很好的文章解决了我的问题。本文解释了为什么在评估从不同程序集的公共方法返回的匿名类型实例时,C4.0的动态特性似乎不起作用。感谢ligaz给了我们一个好的起点