如何使用 Visual Studio 2017 创建参考程序集 [英] How to create a Reference Assembly with Visual Studio 2017

查看:29
本文介绍了如何使用 Visual Studio 2017 创建参考程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create a Reference Assembly with Visual Studio 2017 and not with the command line.

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

I've created a new class library project using Visual Studio 2017 and I've modified the constructor of the default class like this:

using System;

namespace ReferenceAssembly
{
    public class Class1
    {
        public Class1()
        {
            throw new Exception("Hello World");
        }
    }
}

因为我想要一个引用程序集,我希望构造函数的实现像 throw null 而不是 throw new Exception("Hello World");

As I want a reference assembly, I hope the constructor's implementation like throw null and not throw new Exception("Hello World");

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

If I compile the project from the command line:

csc.exe /target:library /refonly /out:referenceAssembly.dll Class1.cs

...一切正常:如果我反编译程序集,我得到的是预期的:

...everything works fine: if I decompile the assembly what I get is as expected:

public Class1()
{
    throw null;
}

现在我想通过 Visual Studio 2017 来实现.

Now I want to do it via Visual Studio 2017.

Visual Studio 2017 c# 属性窗口没有任何标志来指定 refonly 标志,所以我决定编辑 .csproj 文件,添加 true</ProduceOnlyReferenceAssembly> 在每个 PropertyGroup 节点内:

Visual Studio 2017 c# property window doesn't have any flag to specify the refonly flag, so I decided to edit the .csproj file adding <ProduceOnlyReferenceAssembly>true</ProduceOnlyReferenceAssembly> inside each PropertyGroup node:

<?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>binDebug</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>binRelease</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="PropertiesAssemblyInfo.cs" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)Microsoft.CSharp.targets" />
 </Project>

但在使用 Visual Studio 2017 编译后,反编译后得到的是:

But after compiling with Visual Studio 2017 what I get after decompiling is:

public Class1()
{
    throw new Exception("Hello World");
}

推荐答案

如何使用 Visual Studio 创建参考程序集

How to create a Reference Assembly with Visual Studio

解决方法:

在 VS2017 中尝试使用 ProduceReferenceAssembly.然后您可以在 ref 文件夹中找到参考程序集.(或者你可以使用 VS2019,这个问题已经在那里修复了.)

Try using ProduceReferenceAssembly when you're in VS2017. Then you can find the reference assembly in ref folder. (Or you can use VS2019, this issue is fixed there.)

根据 thisdocumentProduceOnlyReferenceAssembly 是一个布尔值,它指示编译器只发出引用程序集而不是编译代码.该属性对应vbc.exe和csc.exe编译器的/refonly开关.

According to this document, ProduceOnlyReferenceAssembly is a boolean value that instructs the compiler to emit only a reference assembly rather than compiled code. This property corresponds to the /refonly switch of the vbc.exe and csc.exe compilers.

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

It's strange that the ProduceOnlyReferenceAssembly failed to work in VS while the command-line worked. I think it could be one issue with VS2017, so I suggest you report this one to DC forum.

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

(I test the property and find it works well in VS2019).

这篇关于如何使用 Visual Studio 2017 创建参考程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆