How to add version info to "Regex.CompileToAssembly" generated DLL

Viewed 25

Newbie looking for some help please.

I am creating a DLL with compiled regular expressions by following this MS article:

https://docs.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.regex.compiletoassembly?view=net-6.0#system-text-regularexpressions-regex-compiletoassembly(system-text-regularexpressions-regexcompilationinfo()-system-reflection-assemblyname-system-reflection-emit-customattributebuilder()-system-string)

The generate DLL is functional and the regular expressions work as expected.

But I would like to be able to control the version information seen in the DLL properties (when you click on Properties from the right-click menu in Windows Explorer). I want the version info to match that of the project executable. So I have tried to pass the version information into the Regex.CompileToAssembly method. But I can’t seem to get this to work. When I look at the properties of the DLL that is generated, I do not see the version information set.

DLL Properties missing version info

How can I get the version property set?

Thanks for any kind help! Dan

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Text.RegularExpressions;

namespace MakeRegExDLL
{
    class MakeRegExDLL
    {
        // We will run this executable as a post-build step to generate the DLL
        static void Main()
        {
            // Local variables
            RegexCompilationInfo regex;
            List<RegexCompilationInfo> regexList = new List<RegexCompilationInfo>();

            regex = new RegexCompilationInfo(
                @"[\dY]{4}-[01M][\dM]-[0123D][\dD]",
                RegexOptions.IgnoreCase,
                "FindDate",
                "MakeRegExDLL",
                true);
            regexList.Add(regex);

            // Copy our regex into compile info array
            RegexCompilationInfo[] regexArray = new RegexCompilationInfo[regexList.Count];
            regexList.CopyTo(regexArray);

            // Get the current assembly object
            string exeFile = System.Reflection.Assembly.GetEntryAssembly().Location;
            AssemblyName currentAssembly = AssemblyName.GetAssemblyName(exeFile);

            // Create the assembly name object for DLL
            string assemblyName = string.Format("{0}, Version={1}, Culture = neutral, PublicKeyToken = null",
                currentAssembly.Name, currentAssembly.Version);
            AssemblyName regexAssembly = new AssemblyName(assemblyName);

            // Setup AssemblyVersionAttribute
            Type[] assemVAParams = { typeof(string) };
            object[] assemVAValues = { currentAssembly.Version.ToString() };
            ConstructorInfo assemVACtor = typeof(AssemblyVersionAttribute).GetConstructor(assemVAParams);

            // Setup AssemblyFileVersionAttribute
            Type[] assemFVAParams = { typeof(string) };
            object[] assemFVAValues = { currentAssembly.Version.ToString() };
            ConstructorInfo assemFVACtor = ypeof(AssemblyFileVersionAttribute).GetConstructor(assemFVAParams);

            // Setup AssemblyProductAttribute
            Type[] assemPAParams = { typeof(string) };
            object[] assemPAValues = { currentAssembly.Version.ToString() };
            ConstructorInfo assemPACtor = typeof(AssemblyProductAttribute).GetConstructor(assemPAParams);

            // Create the CustomAttributeBuilder object array
            CustomAttributeBuilder[] attBuilder = {
                new CustomAttributeBuilder(assemVACtor, assemVAValues),
                new CustomAttributeBuilder(assemFVACtor, assemFVAValues),
                new CustomAttributeBuilder(assemPACtor, assemPAValues) };

            // Create the DLL
            Regex.CompileToAssembly(regexArray, regexAssembly, attBuilder);
        }
    }
}
1 Answers

I ended up using Vestris.ResourceLib to set the DLL property version.

using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
using Vestris.ResourceLib;

namespace MakeRegExDLL
{
    class MakeRegExDLL
    {
        // We will run this executable as a post-build step to generate the DLL
        static void Main()
        {
            // Local variables
            RegexCompilationInfo regex;
            List<RegexCompilationInfo> regexList = new List<RegexCompilationInfo>();

            regex = new RegexCompilationInfo(
                @"[\dY]{4}-[01M][\dM]-[0123D][\dD]",
                RegexOptions.IgnoreCase,
                "FindDate",
                "MakeRegExDLL",
                true);
            regexList.Add(regex);

            // Copy our regex into compile info array
            RegexCompilationInfo[] regexArray = new RegexCompilationInfo[regexList.Count];
            regexList.CopyTo(regexArray);

            // Get the current assembly object
            string exeFile = System.Reflection.Assembly.GetEntryAssembly().Location;
            AssemblyName currentAssembly = AssemblyName.GetAssemblyName(exeFile);

            // Create the assembly name object for DLL
            string assemblyName = string.Format("{0}, Version={1}, Culture = neutral, PublicKeyToken = null",
                currentAssembly.Name, currentAssembly.Version);
            AssemblyName regexAssembly = new AssemblyName(assemblyName);

            // Create the DLL
            Regex.CompileToAssembly(regexArray, regexAssembly);

            // Set the DLL version property
            string dllFile = exeFile.Replace(".exe", ".dll");
            VersionResource versionResource = new VersionResource();
            versionResource.FileVersion = currentAssembly.Version.ToString();
            versionResource.ProductVersion = currentAssembly.Version.ToString();

            // Set other DLL properties
            StringFileInfo stringFileInfo = new StringFileInfo();
            versionResource[stringFileInfo.Key] = stringFileInfo;
            StringTable stringFileInfoStrings = new StringTable();
            stringFileInfoStrings.LanguageID = 1033;
            stringFileInfoStrings.CodePage = 1200;
            stringFileInfo.Strings.Add(stringFileInfoStrings.Key, stringFileInfoStrings);
            stringFileInfoStrings["ProductName"] = currentAssembly.Name;
            stringFileInfoStrings["ProductVersion"] = string.Format("{0}\0", versionResource.ProductVersion);

            // Update the DLL with the new properties
            VarFileInfo varFileInfo = new VarFileInfo();
            versionResource[varFileInfo.Key] = varFileInfo;
            VarTable varFileInfoTranslation = new VarTable("Translation");
            varFileInfo.Vars.Add(varFileInfoTranslation.Key, varFileInfoTranslation);
            varFileInfoTranslation[ResourceUtil.USENGLISHLANGID] = 1300;
            versionResource.SaveTo(dllFile);
        }
    }
}
Related