找不到用于 .NET Standard 的 Convert.ToInt32 方法

Cannot find Convert.ToInt32 method for using with .NET Standard

我正在尝试将项目从 netcoreapp 框架更新到 netstandard 并寻找一种方法来替换从对象到 int 的转换:

int value =  Convert.ToInt32(rawObject);

因为 Convert class 在 CoreCLR 中定义,不能与 .NET Standard 框架一起使用。

project.json:

{
  "version": "1.0.0-*",

  "dependencies": {
    "System.Runtime": "4.1.0",
    "System.Linq": "4.1.0",
    "System.Linq.Expressions": "4.1.0",
    "System.Reflection": "4.1.0",
    "System.Threading.Tasks": "4.0.11",
    "System.Reflection.TypeExtensions": "4.1.0",
    "System.Runtime.Extensions": "4.1.0"
  },

  "frameworks": {
    "netstandard1.6": {
    }
  }
}

更新:

感谢@Lex-Li 和@svick:Convert.ToInt32(object) 方法确实在 System.Runtime.Extensions 参考程序集中定义。 在我的例子中,即使在更新依赖项之后我也有以下错误: "The name 'Convert' does not exist in the current context".

删除project.lock.json并从头开始恢复问题已解决。

将 System.Runtime.Extensions 4.1.0 添加到您的 project.json

在以下网站上快速搜索可以告诉您原因,

http://packagesearch.azurewebsites.net/

你的代码用你的project.json编译对我来说很好。

Convert.ToInt32(object) is defined in the System.Runtime.Extensions reference assembly.

System.Runtime.Extensions 的实现程序集将 System.Convert 转发到 mscorlib(使用 [TypeForwardedTo])。