WP7.1 上的匿名类型和 Get 访问器?
我正在尝试向字典转换器编写一个简单的对象,如下所示:
public static class SimplePropertyDictionaryExtensionMethods
{
public static IDictionary<string,string> ToSimplePropertyDictionary(this object input)
{
if (input == null)
return new Dictionary<string, string>();
var propertyInfos = from property in input.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.GetProperty)
where property.CanRead
select property;
return propertyInfos.ToDictionary(x => x.Name, x => input.GetPropertyValueAsString(x));
}
public static string GetPropertyValueAsString(this object input, PropertyInfo propertyInfo)
{
var value = propertyInfo.GetGetMethod().Invoke(input, new object[] {});
if (value == null)
return string.Empty ;
return value.ToString();
}
}
但是,当我尝试这样调用时:
var test = (new { Foo="12", Bar=15 }).ToSimplePropertyDictionary();
然后它失败并出现异常:
[System.MethodAccessException]: {"Attempt to access the method failed: .<>f__AnonymousType0`1.get_Foo()"}
这只是 Mango 上的安全模型说“不”吗?有什么办法解决吗?感觉这是一个公共 Get 访问器 - 所以感觉我应该能够调用它?
斯图尔特
I'm trying to write a simple object to Dictionary converter like below:
public static class SimplePropertyDictionaryExtensionMethods
{
public static IDictionary<string,string> ToSimplePropertyDictionary(this object input)
{
if (input == null)
return new Dictionary<string, string>();
var propertyInfos = from property in input.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy | BindingFlags.GetProperty)
where property.CanRead
select property;
return propertyInfos.ToDictionary(x => x.Name, x => input.GetPropertyValueAsString(x));
}
public static string GetPropertyValueAsString(this object input, PropertyInfo propertyInfo)
{
var value = propertyInfo.GetGetMethod().Invoke(input, new object[] {});
if (value == null)
return string.Empty ;
return value.ToString();
}
}
However, when I try to call this like:
var test = (new { Foo="12", Bar=15 }).ToSimplePropertyDictionary();
Then it fails with an exception:
[System.MethodAccessException]: {"Attempt to access the method failed: .<>f__AnonymousType0`1.get_Foo()"}
Is this just the security model on Mango saying "No"? Is there any way around it? It feels like this is a public Get accessor - so it feels like I should be able to invoke it?
Stuart
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜您的 ToSimplePropertyDictionary 方法和实际用法位于两个单独的程序集中。这是问题的根源,因为从匿名类生成的编译器生成的类是
内部
。这就是您收到MethodAccessException
异常的原因。因此,您需要使用 InternalsVisibleToAttribute 来让它发挥作用。这个SO问题包含有关内部类型和反射的更多信息。I guess that your
ToSimplePropertyDictionary
method and the actual usage are in two separate assemblies. This is the source of your problem because the compiler generated class which is generated from an anonymous class isinternal
. That is why you get theMethodAccessException
exception. So you need to use the InternalsVisibleToAttribute to make it work. This SO question contains more info about internal types and reflection.删除 BindingFlags.GetProperty
这用于在使用 InvokeMember 时获取属性值,它不指定您希望返回只读属性。
编辑:问题实际上可能出在 propertyInfo.GetGetMethod() - 尝试使用以下之一(我只使用过第一个):
Remove BindingFlags.GetProperty
This is used to get a property value when using InvokeMember, it does not specify that you want a read only property returned.
EDIT: The problem may actually be with the propertyInfo.GetGetMethod() - Try using one of the following (I have only ever used the first one):