C# 在UWP应用程序的PCL中使用ConcurrentDictionary

C# 在UWP应用程序的PCL中使用ConcurrentDictionary,c#,.net,portable-class-library,uwp,concurrentdictionary,C#,.net,Portable Class Library,Uwp,Concurrentdictionary,我最近遇到了一个问题,想解释一下原因: 在使用Profile5(.Net 4,Windows 8)的可移植类库(PCL)中,我有以下代码: using System.Collections.Concurrent; public class Foo { public static void Bar(ConcurrentDictionary<string, bool> dict) { dict.AddOrUpdate("true", true, (k,

我最近遇到了一个问题,想解释一下原因:

在使用Profile5(.Net 4,Windows 8)的可移植类库(PCL)中,我有以下代码:

using System.Collections.Concurrent;

public class Foo
{
    public static void Bar(ConcurrentDictionary<string, bool> dict)
    {
        dict.AddOrUpdate("true", true, (k, v) => true);
    }

    public static void Baz()
    {
        new ConcurrentDictionary<string, bool>();
    }
}
它会编译,但会在以下情况下崩溃:

找不到方法:“Void Foo.Bar(System.Collections.Concurrent.ConcurrentDictionary`2)”

对Baz的调用会以不同的方式失败:

方法“Foo.Baz()”尝试访问方法“System.Collections.Concurrent.ConcurrentDictionary`2..ctor()”失败

因此,在这种情况下,
TypeForwardedTo
magic似乎不起作用,或者丢失了。编译器知道它应该是相同的类型(即使存在于不同的程序集中),但运行时不知道

在使用反射的运行时,我可以看到实际上有2种
ConcurrentDictionary
类型:

  • System.Collections.Concurrent.ConcurrentDictionary'2[[TKey,TValue]],System.Collections.Concurrent,Version=4.0.10.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a
    当我使用
    typeof(ConcurrentDictionary)
  • System.Collections.Concurrent.ConcurrentDictionary'2[[TKey,TValue]],mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e
    当我在
    Bar
    参数上使用反射时
将PCL更改为使用4.5而不是4.0(其中引用了新的
系统.*
库)的配置文件可以使一切正常工作

我不明白的是,为什么不像大多数其他情况下那样,没有某种形式的映射?是什么阻止了微软创建它

(将“可移植”库定义为不可移植是相当令人困惑的。尤其是当它们可以是半可移植的并且只有在运行时到达部分代码时才会崩溃时)


注意:我不是在寻找解决方法,我不会使用
ConcurrentDictionary
,(或者不使用UWP)对于这个项目,我更想寻找一个解释,解释为什么UWP和PCL之间存在如此大的可用性问题。

我不得不尝试去相信它。真是一团糟。。。我想这就解释了为什么像json.net这样的最新nuget包为PCL4.0和PCL4.5提供了不同的二进制文件
var dict = new ConcurrentDictionary<string, bool>();
Foo.Bar(dict);