Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Output Assembly Filename (csproj)

Is there any way to change the output assembly filename when building by 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.

Please, don't advice to change AssemblyName parameter (the only hint I saw by googling the SO), they are not the same. AssemblyName also changes the full classname for every class and this is not desired. 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. 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.

UPD: Clearer explanation, why the AssemblyName change is not what I want. 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. For example, I reference ProgName.dll for ISome interface. The resolver tries to find ProgName (as set in type's metadata) and founds nothing, because there is no ProgName, only ProgName.Test. When they all have AssemblyName ProgName (not .Test), it will work. Of course, if I deal with different filenames.

UPD2: Even better explanation of what I meant when said about the AssemblyName: Resolver data screenshot 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).

I've used ILRepack to merge several assemblies into one:

  • A.dll
  • B.dll (or B.Test.dll, depending on configuration)
  • C.dll

Both A and C reference B.dll. If I merge B.Test.dll instead (with AssemblyName "B.Test") the references will remain references. If I merge B.dll, no references remain (all code inside). So I need B.Test.dll (with this filename) to have AssemblyName "B".

like image 845
rattler Avatar asked Oct 23 '16 14:10

rattler


1 Answers

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.

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.

This doesn't change any class name or the namespaces which you're assuming.

like image 159
Jan Köhler Avatar answered Sep 30 '22 01:09

Jan Köhler