Dictionary 初始值设定项中的 KeyValuePair 类型推断
KeyValuePair type inference inside Dictionary initializer
我正在构建密钥类型为 KeyValuePair
的 Dictionary
。当在 Dictionary
初始值设定项中使用 KeyValuePair.Create
时,它不需要模板类型并且它正确推断类型,以下编译正确甚至没有警告:
private static readonly IDictionary<KeyValuePair<Market, MarketData>, string> channelDict =
new Dictionary<KeyValuePair<Market, MarketData>, string> {
{ KeyValuePair.Create(Market.USD, MarketData.Trade), "trades" },
...
};
现在代码被重组,类型 Market
和 MarketData
被移动到一个单独的项目和命名空间,命名空间由文件顶部的 using
语句导入,但现在无法编译相同的代码,它会抛出此错误:
error CS0305: Using the generic type 'KeyValuePair<TKey, TValue>' requires 2 type arguments
我可以确定 using
命名空间导入正确,因为简单的添加 private Market _m;
没有产生任何错误,更重要的是,字典的定义:
private static readonly IDictionary<KeyValuePair<Market, MarketData>, string> channelDict =
new Dictionary<KeyValuePair<Market, MarketData>, string>
也没有报错,是Create
方法无法编译:
KeyValuePair.Create(Market.USD, MarketData.Trade)
那么为什么当类型参数在另一个命名空间中时它现在不能推断类型,而以前可以?
经过一番挖掘,我才发现问题不在于 KeyValuePair
的初始化,而是因为目标框架的差异。
最初所有代码都在同一个 .NET Core 控制台应用程序中,将目标框架设置为 netcoreapp2.0
,从文档中,在 .NET Core 2.0 中,KeyValuePair
is a static class with a static method Create
,这将推断正确输入参数。
然后当我重组代码时,我将这部分移动到一个class库中,默认情况下,dotnet工具将设置netstandard2.0
作为它的目标框架,然而,KeyValuePair
and Create
is not available in netstandard2.0
, as you can see from docs,它说:
This API is not supported in the currently selected framework.
这就是代码在移动到库项目时编译失败的原因,netstandard
框架中只有通用 KeyValuePair<K, V>
版本。
作为变通方法,我验证了当我将 .csproj 文件中的目标框架设置为 netcoreapp2.0
并将 outputtype
设置为 library
时,编译成功。
OTOH,库项目默认使用 netstandard,因为它确保库在不同平台目标之间可用,所以更好的解决方法是不要在库代码中使用 static KeyValuePair.Create
,而是使用通用版本: new KeyValuePair<K, V>(k, v)
.
我正在构建密钥类型为 KeyValuePair
的 Dictionary
。当在 Dictionary
初始值设定项中使用 KeyValuePair.Create
时,它不需要模板类型并且它正确推断类型,以下编译正确甚至没有警告:
private static readonly IDictionary<KeyValuePair<Market, MarketData>, string> channelDict =
new Dictionary<KeyValuePair<Market, MarketData>, string> {
{ KeyValuePair.Create(Market.USD, MarketData.Trade), "trades" },
...
};
现在代码被重组,类型 Market
和 MarketData
被移动到一个单独的项目和命名空间,命名空间由文件顶部的 using
语句导入,但现在无法编译相同的代码,它会抛出此错误:
error CS0305: Using the generic type 'KeyValuePair<TKey, TValue>' requires 2 type arguments
我可以确定 using
命名空间导入正确,因为简单的添加 private Market _m;
没有产生任何错误,更重要的是,字典的定义:
private static readonly IDictionary<KeyValuePair<Market, MarketData>, string> channelDict =
new Dictionary<KeyValuePair<Market, MarketData>, string>
也没有报错,是Create
方法无法编译:
KeyValuePair.Create(Market.USD, MarketData.Trade)
那么为什么当类型参数在另一个命名空间中时它现在不能推断类型,而以前可以?
经过一番挖掘,我才发现问题不在于 KeyValuePair
的初始化,而是因为目标框架的差异。
最初所有代码都在同一个 .NET Core 控制台应用程序中,将目标框架设置为 netcoreapp2.0
,从文档中,在 .NET Core 2.0 中,KeyValuePair
is a static class with a static method Create
,这将推断正确输入参数。
然后当我重组代码时,我将这部分移动到一个class库中,默认情况下,dotnet工具将设置netstandard2.0
作为它的目标框架,然而,KeyValuePair
and Create
is not available in netstandard2.0
, as you can see from docs,它说:
This API is not supported in the currently selected framework.
这就是代码在移动到库项目时编译失败的原因,netstandard
框架中只有通用 KeyValuePair<K, V>
版本。
作为变通方法,我验证了当我将 .csproj 文件中的目标框架设置为 netcoreapp2.0
并将 outputtype
设置为 library
时,编译成功。
OTOH,库项目默认使用 netstandard,因为它确保库在不同平台目标之间可用,所以更好的解决方法是不要在库代码中使用 static KeyValuePair.Create
,而是使用通用版本: new KeyValuePair<K, V>(k, v)
.