3

JSON.NET を使用して LINQPad でこのコードを実行すると:

var x = JObject.Parse(
@"{
  ""data"" : [ {
    ""id"" : ""bbab529ecefe58569c2b301a"",
    ""name"" : ""Sample Name"",
    ""group"" : ""8b618be8dc064e653daf62f9"",
    ""description"" : ""Sample Name"",
    ""payloadType"" : ""Geolocation"",
    ""contract"" : ""a9da09a7f4a7e7becf961865"",
    ""keepAlive"" : 0
  } ]
}");

x.Dump();

AmbiguousMatchException解析された JSON を LINQPad の出力ウィンドウにダンプしようとすると、anがスローされます。なんで?私が知る限り、これは完全に正当な JSON です。http://jsonlint.com/も有効だと言っています。

4

1 に答える 1

4

これは、.Dump()実装方法に問題がある可能性が最も高いです。

スタック トレースを確認すると、次のようになります。

at System.RuntimeType.GetInterface(String fullname, Boolean ignoreCase)
at System.Type.GetInterface(String name)
at UserQuery.Main()
...

例外をスローするメソッドが であることがわかりますSystem.RuntimeType.GetInterface

System.RuntimeTypeTypeは、実行時にリフレクションが使用されるときにオブジェクトを表すために使用される具体的なクラスの 1 つですType.GetInterface(String, Boolean)

AmbiguousMatchException
現在の Type は、異なる型引数を持つ同じジェネリック インターフェイスを実装する型を表しています。

そのため、GetInterfaceメソッドは、複数回実装されているインターフェイスのタイプで呼び出され、異なるTや類似のもので呼び出されているようです。

同じエラーを引き起こすには、単純に次のように置き換えますx.Dump();

var type = x.GetType().GetInterface("System.Collections.Generic.IEnumerable`1", true);

これにより、同じ例外がスローされます。

根本的な問題を示す単純なLINQPadの例を次に示します。

void Main()
{
    var type = typeof(Problem).GetInterface("System.Collections.Generic.IEnumerable`1", true);
}

public class Problem : IEnumerable<string>, IEnumerable<int>
{
    IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable<string>)this).GetEnumerator();
    IEnumerator<string> IEnumerable<string>.GetEnumerator() => Enumerable.Empty<string>().GetEnumerator();
    IEnumerator<int> IEnumerable<int>.GetEnumerator() => Enumerable.Empty<int>().GetEnumerator();
}

この例では、まったく同じ例外がスローされます。


結論: Json にも Json.Net にも問題はありません。これは、LINQPad がオブジェクトを出力ウィンドウにダンプする最善の方法を見つけようとする方法に問題があります。

于 2016-09-02T07:41:33.643 に答える