Generating code via codedom and assigning version to assembly

I have an application which programatically builds and compiles DLLs using Codedom. I am building the source code using a stringbuilder and then using the CompileAssemblyFromSource option to compile the DLL. I would like to be able to set the AssemblyVersion attribute for my generated DLL so that I can right-click on the DLL and see the version number property listed. Currently it just shows as version 0.0.0.0. It would be nice to set the other attributes as well, my I'm mostly concerned with the version number.

So far in my research I see references to objects such as the CodeAttributeDeclaration, but can't find any good sample code and can't figure out how to set the version number for the assembly.

Any help would be appreciated. Thanks.


Answer this question

Generating code via codedom and assigning version to assembly

  • Dan_12345

    That did the trick. Thank you very much.
  • N2H

    Ok, sorry about that.  You started off saying you used the CodeDom.  I guess I missed where you said you used CompileAssemblyFromSource.

    The syntax for the version attribute is[assembly: AssemblyVersion("5.4.3.21")]You should add it after any using statements, but outside of any namespaces.  You get a compiler error if you include an assembly-level attribute inside a namespace.

  • SDY

    You found the right class.  Here's how to use a CodeAttributeDeclaration.
    string versionn = "5.4.3.21";
     
    CodeAttributeArgument arg = new CodeAttributeArgument(new CodePrimitiveExpression(version));
      
    CodeAttributeDeclaration attribute = new CodeAttributeDeclaration("System.Reflection.AssemblyVersionAttribute", arg);
      
    compileUnit.AssemblyCustomAttributes.Add(attribute);
    Assembly-level attributes get added using the AssemblyCustomAttributes property of the CodeCompileUnit, as shown above.  Regular attributes would get added using the CustomAttributes property of whatever you're adding the attribute to (CodeTypeDeclaration, CodeMemberMethod, etc.).

  • Conor Morrison - MSFT

    Thanks. But I am still not quite there yet. My code is not using a CompileUnit, CodetypeDeclaration etc. object currently. It is building the code by populating a stringbuilder and compiling from there. It's done this way instead of using all the various CodeDom objects because that's the way the original author of the code wrote it. 

    Once I do the code shown above, what do I do with it

  • Generating code via codedom and assigning version to assembly