C# 如何使用Visual Studio 2017创建引用程序集

C# 如何使用Visual Studio 2017创建引用程序集,c#,.net,visual-studio,msbuild,.net-assembly,C#,.net,Visual Studio,Msbuild,.net Assembly,我正在尝试使用Visual Studio 2017而不是命令行创建引用程序集 我使用Visual Studio 2017创建了一个新的类库项目,并修改了默认类的构造函数,如下所示: using System; namespace ReferenceAssembly { public class Class1 { public Class1() { throw new Exception("Hello World");

我正在尝试使用Visual Studio 2017而不是命令行创建引用程序集

我使用Visual Studio 2017创建了一个新的类库项目,并修改了默认类的构造函数,如下所示:

using System;

namespace ReferenceAssembly
{
    public class Class1
    {
        public Class1()
        {
            throw new Exception("Hello World");
        }
    }
}
因为我想要一个引用程序集,所以我希望构造函数的实现像
抛出null
,而不是
抛出新的异常(“Hello World”)

如果我从命令行编译项目:

csc.exe /target:library /refonly /out:referenceAssembly.dll Class1.cs
…一切正常:如果我反编译程序集,得到的结果与预期一致:

public Class1()
{
    throw null;
}
现在我想通过Visual Studio 2017来实现这一点

Visual Studio 2017 c#属性窗口没有任何标志来指定REONLY标志,因此我决定编辑.csproj文件,在每个PropertyGroup节点中添加
true

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <ProduceOnlyReferenceAssembly>true</ProduceOnlyReferenceAssembly>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>3be579e4-9fde-4fd1-867c-ac5cd0411b65</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>ReferenceAssembly</RootNamespace>
    <AssemblyName>ReferenceAssembly</AssemblyName>
    <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <ProduceOnlyReferenceAssembly>true</ProduceOnlyReferenceAssembly>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <ProduceOnlyReferenceAssembly>true</ProduceOnlyReferenceAssembly>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System"/>
    <Reference Include="System.Core"/>
    <Reference Include="System.Xml.Linq"/>
    <Reference Include="System.Data.DataSetExtensions"/>
    <Reference Include="Microsoft.CSharp"/>
    <Reference Include="System.Data"/>
    <Reference Include="System.Net.Http"/>
    <Reference Include="System.Xml"/>
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Class1.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>
如何使用Visual Studio创建引用程序集

解决方法:

在VS2017中尝试使用
produceReferencesAssembly
。然后您可以在
ref
文件夹中找到参考程序集。(也可以使用VS2019,此问题已在此处修复。)

根据,
ProduceOnlyReferenceAssembly
是一个布尔值,指示编译器仅发出引用程序集而不是已编译代码。此属性对应于
vbc.exe和csc.exe
编译器的
/refonly
开关

奇怪的是,
ProduceOnlyReferenceAssembly
在命令行工作时无法在VS中工作。我认为这可能是VS2017的一个问题,因此我建议您将此问题报告给


(我测试了该属性,发现它在VS2019中运行良好)。

哪个VS版本?2017年。我将编辑这个问题。
public Class1()
{
    throw new Exception("Hello World");
}