how to set a version on dynamically created dll

I need some help with setting a version on a dynamically created dll.

I’m creating the dll using the CodeDomProvider and I can’t set the version of the output assembly.

I’ve seen in some blogs that it can be done only if you set the compiler parameters to generate an executable and not in-memory.

my code is:

using System;
using System.CodeDom.Compiler;
using System.Diagnostics;
using System.Linq;

namespace dynamicDll
{
    class Program
    {
        static void Main(string[] args)
        {
            CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateInMemory = true;
            parameters.GenerateExecutable = false;
            parameters.IncludeDebugInformation = true;
            parameters.OutputAssembly = "Dynamic.dll";
            var assemblies = AppDomain.CurrentDomain.GetAssemblies()
                            .Where(a => !a.IsDynamic)
                            .Select(a => a.Location);
            parameters.ReferencedAssemblies.AddRange(assemblies.ToArray());

            CompilerResults cr = CodeDomProvider.CreateProvider("CSharp")
                .CompileAssemblyFromSource(parameters, @"          
namespace Dynamic
                    {
                        public class DynamicDllClass
                        {
                            public int X { get {return 7;}}
                        }
                    }");
            cr.CompiledAssembly.GetName().Version = new Version("1.0.0.0");
            var instance = cr.CompiledAssembly.CreateInstance("Dynamic.DynamicDllClass");
            int x = (int) instance.GetType().GetProperty("X").GetGetMethod().Invoke(instance, null);
            Debug.Assert(x==7);
            Debug.Assert(cr.CompiledAssembly.FullName.Contains("1.0.0.0"));
        }
    }
}


Source: dll

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.