简体   繁体   English

C#输出程序集文件名(csproj)

[英]C# Output Assembly Filename (csproj)

Is there any way to change the output assembly filename when building by VS? 有没有办法在VS构建时更改输出程序集文件名?

I have two (four, including Debug/Release branches) configurations with a bit different functionality and that should be used differently, but should remain in same dir. 我有两个(四个,包括调试/发布分支​​)配置,具有不同的功能,应该以不同的方式使用,但应保留在同一个目录中。 So, the best are ProjectName.dll and ProjectName.Test.dll. 所以,最好的是ProjectName.dll和ProjectName.Test.dll。

Please, don't advice to change AssemblyName parameter (the only hint I saw by googling the SO), they are not the same. 请不要建议更改AssemblyName参数(我通过谷歌搜索SO看到的唯一提示),它们不一样。 AssemblyName also changes the full classname for every class and this is not desired. AssemblyName还会更改每个类的完整类名,但这不是必需的。 I want to change only the filename and remain anything inside assembly unthouched. 我想更改文件名并保留在程序集内部的任何内容。

Now I've done this by adding AfterBuild target into .csproj that copies assemblies with rename, but i don't like this solution. 现在我通过将AfterBuild目标添加到.csproj中来完成此操作,该目标使用重命名复制程序集,但我不喜欢这个解决方案。 May it be easier? 可能更容易吗?

Also tried to set OutputPath to SomeWhere\\ProjectName.Test.dll - it simply creates dir ProjectName.Test.dll and places $(AssemblyName).dll there. 还尝试将OutputPath设置为SomeWhere \\ ProjectName.Test.dll - 它只是创建dir ProjectName.Test.dll并将$(AssemblyName).dll放在那里。

UPD: Clearer explanation, why the AssemblyName change is not what I want. UPD:更清楚的解释,为什么AssemblyName更改不是我想要的。 AssemblyName is also the part of every type in assembly (it points to the assembly, from where is type is) and it directly applies to type resolve. AssemblyName也是程序集中每个类型的一部分(它指向程序集,类型是where),它直接适用于类型resolve。 For example, I reference ProgName.dll for ISome interface. 例如,我为ISome接口引用了ProgName.dll。 The resolver tries to find ProgName (as set in type's metadata) and founds nothing, because there is no ProgName, only ProgName.Test. 解析器尝试查找ProgName(在类型的元数据中设置)并且什么也没找到,因为没有ProgName,只有ProgName.Test。 When they all have AssemblyName ProgName (not .Test), it will work. 当它们都具有AssemblyName ProgName(而不是.Test)时,它将起作用。 Of course, if I deal with different filenames. 当然,如果我处理不同的文件名。

UPD2: Even better explanation of what I meant when said about the AssemblyName: UPD2:当谈到AssemblyName时,更好地解释了我的意思: 解析器数据截图 The red-marked is what assembly resolver tries to find. 红色标记是程序集解析器试图找到的。 Assembly in both of my configurations have the same interface, so the client should not care about what assembly he need to find. 我的两个配置中的程序集具有相同的接口,因此客户端不应该关心他需要查找的程序集。 Both should have the same AssemblyName, but different filename (to be stored in same folder). 两者都应具有相同的AssemblyName,但文件名不同(存储在同一文件夹中)。

I've used ILRepack to merge several assemblies into one: 我使用ILRepack将几个程序集合并为一个:

  • A.dll A.DLL
  • B.dll (or B.Test.dll, depending on configuration) B.dll(或B.Test.dll,取决于配置)
  • C.dll C.dll

Both A and C reference B.dll. A和C都参考B.dll。 If I merge B.Test.dll instead (with AssemblyName "B.Test") the references will remain references. 如果我合并B.Test.dll(使用AssemblyName“B.Test”),引用将保持引用。 If I merge B.dll, no references remain (all code inside). 如果我合并B.dll,则没有引用(内部所有代码)。 So I need B.Test.dll (with this filename) to have AssemblyName "B". 所以我需要B.Test.dll(使用此文件名)才能拥有AssemblyName“B”。

Just to illustrate what Luc already mentioned in his comment: You could easily tweak some lines in your csproj -file and achieve what you're looking for. 只是为了说明Luc在他的评论中已经提到的内容:你可以轻松调整你的csproj文件中的一些行并实现你正在寻找的东西。

Please take a look at this sample: 请看一下这个样本:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <someProps>....</someProps>
    <RootNamespace>Your.Project</RootNamespace> <!-- Your project namespace stays the same -->
    <AssemblyName>Your.Project</AssemblyName>
    <someMoreProps>...</someMoreProps>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <AssemblyName>Your.Project.Foo</AssemblyName> <!-- you could simply "override" the resulting assembly name -->
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <samePropsAsAbove>...</samePropsAsAbove>
    <AssemblyName>Your.Project.Bar</AssemblyName> <!-- you could do this on a per-configuration-basis -->
  </PropertyGroup>
  ...

By doing so you're simply changing the resulting output file. 通过这样做,您只需更改生成的输出文件。 So in this example the output (using debug configuration) would be Your.Project.Foo.dll instead of the default Your.Project.dll . 因此,在此示例中,输出(使用调试配置)将是Your.Project.Foo.dll而不是默认的Your.Project.dll

This doesn't change any class name or the namespaces which you're assuming. 这不会更改您假设的任何类名或命名空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM