.Net标准1.6动态调度可能在c#中?

时间:2017-05-27 10:29:01

标签: c# dynamic .net-core visual-studio-2017 .net-standard

我正在尝试使用Visual Studio 2017s新的.csproj格式定位多个框架。

我写了一个小型的C#控制台应用程序:

public class Program
{
    public static void Main(string[] args)
    {
        object val = 1;
        PrintMe((dynamic)val);
        val = "test";
        PrintMe((dynamic)val);
        Console.ReadLine();
    }

    public static void PrintMe(int i)
    {
        Console.WriteLine($"integer: {i}");
    }

    public static void PrintMe(string s)
    {
        Console.WriteLine($"string: {s}");
    }
}

在定位.Net Core 1.1时工作正常 - 但如果我尝试通过将.csproj文件更改为:

来多目标.Net Standard 1.6
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup Label="Configuration" Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <OutputType>exe</OutputType>
  </PropertyGroup>

  <PropertyGroup>
    <TargetFrameworks>netcoreapp1.1;netstandard1.6</TargetFrameworks>
    <ApplicationIcon />
    <OutputTypeEx>exe</OutputTypeEx>
    <StartupObject />
  </PropertyGroup>

</Project>

然后在编译时我收到错误:

Error occurred while restoring NuGet packages: The operation failed as details for project DynamicDipsatch could not be loaded.
1>------ Build started: Project: DynamicDipsatch, Configuration: Debug Any CPU ------
1>DynamicDipsatch -> D:\dev\projects\bittercoder\NetCoreExperiments\DynamicDipsatch\bin\Debug\netcoreapp1.1\DynamicDipsatch.dll
1>Class1.cs(10,4,10,25): error CS0656: Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'
1>Done building project "DynamicDipsatch.csproj" -- FAILED.
========== Build: 0 succeeded or up-to-date, 1 failed, 0 skipped ==========

将nuget包引用添加到Microsoft.CSharp或System.Dynamic.Runtime,例如。

<ItemGroup>
  <PackageReference Include="Microsoft.CSharp" Version="4.3.0" />
  <PackageReference Include="System.Dynamic.Runtime" Version="4.3.0" />
</ItemGroup>

也无法解决问题(如针对vs2015的某些类似问题中所示,或者在此微软连接问题中找到 - &gt; https://connect.microsoft.com/VisualStudio/feedback/details/1630573/missing-compiler-required-member-microsoft-csharp-runtimebinder-csharpargumentinfo-create

在定位.net标准1.6时是否可以通过这种方式执行动态调度,如果是这样的话?

1 个答案:

答案 0 :(得分:4)

要为.net标准构建项目,请向Microsoft.CSharp NuGet包添加包引用:

<ItemGroup>
  <PackageReference Include="Microsoft.CSharp" Version="4.3.0" />
</ItemGroup>

另请注意,您的项目只能在调试模式下执行。在主属性组中设置<OutputType>,而不是Debug的条件(看起来像项目系统错误)。

此外,.net标准项目并不意味着执行,因此具有Exe输出类型对netstandard项目并不真正有用。