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

.Net standard 1.6 dynamic dispatch possible in c#?

我正在尝试使用 Visual Studio 2017 年新的 .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 ==========

添加对 Microsoft.CSharp 或 System.Dynamic.Runtime 的 nuget 包引用,例如

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

也没有解决问题(如 vs2015 的一些类似问题或此 Microsoft 连接问题 -> https://connect.microsoft.com/VisualStudio/feedback/details/1630573/missing-compiler-required-member-microsoft-csharp-runtimebinder-csharpargumentinfo-create

以 .net standard 1.6 为目标时,是否可以通过这种方式执行动态调度?如果可以,如何执行?

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

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

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

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