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

时间:2015-10-24 22:44:05

标签: 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, v) => true);
    }

    public static void Baz()
    {
        new ConcurrentDictionary<string, bool>();
    }
}

在Windows 10 UWP应用程序中使用Bar,如下所示:

var dict = new ConcurrentDictionary<string, bool>();
Foo.Bar(dict);

它编译但崩溃:

  

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

对Baz的调用与:

的不同
  

尝试通过方法'Foo.Baz()'访问方法'System.Collections.Concurrent.ConcurrentDictionary`2..ctor()'失败。

所以看起来TypeForwardedTo魔法在这种情况下不起作用,或者遗漏了。编译器理解它应该是相同的类型(即使存在于不同的程序集中),但运行时不会。

在使用反射的运行时,我可以看到实际上有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参数上使用反射时。{/ li>

将PCL更改为使用4.5而不是4.0的配置文件(引用新的System.*库的位置)使一切正常工作。

我没有得到的是为什么没有像大多数其他情况那样存在某种形式的映射?什么阻止微软创建它?

(让“便携式”库不可移植是非常令人困惑的。特别是当它们可以是半便携式并且只在运行时到达某些部分代码时崩溃)

注意 :我不是在寻找解决方法,我没有ConcurrentDictionary,(或没有UWP)这个项目,我正在寻找解释为什么UWP和PCL之间存在如此大的可用性问题的解释。

0 个答案:

没有答案
相关问题