Search code examples
c#visual-studioc-preprocessorvisual-studio-2005assemblies

How can I create different DLLs in one project?


I have a question I don't know if it can be solved. I have one C# project on Visual Studio 2005 and I want to create different DLL names depending on a preprocessor constant. What I have in this moment is the preprocessor constant, two snk files and two assembly's guid. I also create two configurations (Debug and Debug Preprocessor) and they compile perfectly using the snk and guid appropriate.

#if PREPROCESSOR_CONSTANT
[assembly: AssemblyTitle("MyLibraryConstant")]
[assembly: AssemblyProduct("MyLibraryConstant")]
#else
[assembly: AssemblyTitle("MyLibrary")]
[assembly: AssemblyProduct("MyLibrary")]
#endif

Now, I have to put the two assemblies into the GAC. The first assembly is added without problems but the second isn't.

What can I do to create two or more different assemblies from one Visual Studio project?

It's possible that I forgot to include a new line on "AssemblyInfo.cs" to change the DLL name depending on the preprocessor constant?


Solution

  • Here is a sample Post-Build event:

    if "$(ConfigurationName)" == "Debug" goto Debug
    if "$(ConfigurationName)" == "Release" goto Release
    
    goto End
    
    :Debug
    del "$(TargetDir)DebugOutput.dll"
    rename "$(TargetPath)" "DebugOutput.dll"
    
    :Release
    del "$(TargetDir)ReleaseOutput.dll"
    rename "$(TargetPath)" "ReleaseOutput.dll"
    
    :End
    

    Change DebugOutput.dll and ReleaseOutput.dll to the proper filenames. you can change "Debug" and "Release" to support other configurations, and add sections to support more configurations.


    That script will create two dlls with different file names; to create two different AssemblyNames, you have two options.

    The assembly name is built as follows:

    Name <,Culture = CultureInfo> <,Version = Major.Minor.Build.Revision> <, StrongName> <,PublicKeyToken> '\0'
    

    So you have to either change the culture info, the version or the strong name.

    The two simplest options are:

    1. Change the assembly version:

      #if SOME_COMPILER_SYMBOL
      [assembly: AssemblyVersion("1.0.0.0")]
      #else
      [assembly: AssemblyVersion("1.0.0.1")]
      #endif
      
    2. Change the keyfile - create two keyfiles with the sn tool, and in AssemblyInfo.cs:

      #if SOME_COMPILER_SYMBOL
      [assembly: AssemblyKeyFile("FirstKey.snk")]
      #else
      [assembly: AssemblyKeyFile("SecondKey.snk")]
      #endif
      

    Both these options will result in two different assemblies as far as the GAC knows - since it compares the full assembly name, and not the 'simple' name.