は、私は以下のように辞書コンバータへの単純なオブジェクトを作成しようとしています:WP7.1で匿名の型とアクセサを取得しますか?
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()"}
マンゴーのセキュリティモデルは「いいえ」というだけですか?その周りには道がありますか?これは公共のような感じですアクセサーを取得 - それは私がそれを呼び出すことができるように感じる?
スチュアート
ありがとうございました!それは良い答えのように聞こえます – Stuart
私たちには勝者があります - ありがとう! – Stuart
mmm、良い答えです。私はプロパティを取得するときにBindingFlags.Publicが渡されるので、これを考えなかったでしょう。私は、プロパティが公開されていることをMSDNが示しているにもかかわらず、匿名型が内部である場合、プロパティは返されないと考えていました(親クラスがそうでない場合は奇妙に見えます)。 – calum