Jul*_*lia 5 .net c# portable-class-library concurrentdictionary uwp
我最近遇到了一个问题,想解释一下原因:
在使用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>();
}
}
Run Code Online (Sandbox Code Playgroud)
Bar在 Windows 10 UWP 应用程序中使用,如下所示:
var dict = new ConcurrentDictionary<string, bool>();
Foo.Bar(dict);
Run Code Online (Sandbox Code Playgroud)
它编译但崩溃:
找不到方法:'Void Foo.Bar(System.Collections.Concurrent.ConcurrentDictionary`2)'。
对 Baz 的调用以不同的方式失败:
尝试通过方法 'Foo.Baz()' 访问方法 'System.Collections.Concurrent.ConcurrentDictionary`2..ctor()' 失败。
所以看起来TypeForwardedTo魔法在这种情况下不起作用,或者消失了。编译器理解它应该是相同的类型(即使存在于不同的程序集中)但运行时没有。
在运行时使用反射,我可以看到确实有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(System.*引用新库的地方)的配置文件可使一切正常工作。
我不明白的是,为什么不像大多数其他情况那样有某种形式的映射?是什么阻止微软创建它?
(让“可移植”库不可移植是非常令人困惑的。尤其是当它们可以是半可移植的并且只有在运行时到达代码的某些部分时才会崩溃)
笔记:我不是在寻找解决方法,我会在没有ConcurrentDictionary,(或没有 UWP)这个项目中做,我更多的是在寻找解释为什么 UWP 和 PCL 之间存在如此大的可用性问题。