How to create a Reference Assembly with Visual Studio 2017

痞子三分冷 提交于 2021-01-29 10:04:54

问题


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

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");
        }
    }
}

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;
}

Now I want to do it via Visual Studio 2017.

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>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>

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

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

回答1:


How to create a Reference Assembly with Visual Studio

Workaround:

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.)

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.

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.

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



来源:https://stackoverflow.com/questions/58900429/how-to-create-a-reference-assembly-with-visual-studio-2017

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!