0

In my code, I create a DLL dynamically. The following code is what I use to create my DLL:

/// <summary>
/// See CodeDom example hon how its used
/// </summary>
/// <param name="source_code"></param>
/// <param name="assemblies"></param>
/// <param name="generate_executable"></param>
/// <param name="compiler_options"></param>
/// <param name="output"></param>
/// <returns></returns>
public static Tuple<Assembly, string> CompileSource(string source_code, List<string> assemblies, bool generate_executable = false, string compiler_options = "/optimize", string output_assembly = "")
{
    CompilerParameters compilerParams = null;
    CSharpCodeProvider provider = null;
    CompilerResults result = null;
    string errors = string.Empty;

    try
    {
        provider = new CSharpCodeProvider();
        compilerParams = new CompilerParameters();
        foreach (var entry in assemblies)
        {
            compilerParams.ReferencedAssemblies.Add(entry);
        }
        if (output_assembly != string.Empty)
            compilerParams.OutputAssembly = output_assembly;

        compilerParams.GenerateExecutable = generate_executable;
        compilerParams.GenerateInMemory = false;
        compilerParams.IncludeDebugInformation = true;
        compilerParams.CompilerOptions = compiler_options;

        result = provider.CompileAssemblyFromSource(compilerParams, source_code);

        foreach (CompilerError error in result.Errors)
        {
            errors += String.Format("{0}({1},{2}: error {3}: {4}", error.FileName, error.Line, error.Column, error.ErrorNumber, error.ErrorText);
        }

    }
    catch (Exception err)
    {
    }

    return Tuple.Create(result.CompiledAssembly, errors);


}

What I would like to do is add Assembly Information like the company, product, title, etc. Looking at this site and site to add it but I'm not sure how to do it.

enter image description here

The CompilerParameters doesn't seem to give an option to add these attributes.

Anyone have a suggestion if it can be done on how to add this information?

Thanks

adviner
  • 3,295
  • 10
  • 35
  • 64
  • https://stackoverflow.com/a/17112163/8967612 – 41686d6564 stands w. Palestine Jan 29 '20 at 20:18
  • I saw that but the problem is I pass the code as a string like the following example: string code = @"var date = DateTime.Parse(""1/1/2013 11:54 AM""); " + @"retVal = date.ToString(""MMMM"") + "" "" + date.ToString(""dd"") + "", "" + date.ToString(""yyyy"");"; – adviner Jan 29 '20 at 20:25
  • I tried adding @"[assembly: AssemblyCompany(""Pain Company"")]" in the string but it fails – adviner Jan 29 '20 at 20:26
  • Make sure you add it at the top of the file and to also call `using System.Reflection;`. Here's a [full example](https://pastebin.com/6sCBZkq6) that works just fine for me using your method above. – 41686d6564 stands w. Palestine Jan 29 '20 at 20:59
  • Thank you Ahmed, I got it to work. I appreciate the advice – adviner Jan 29 '20 at 21:32
  • *This may help:* > [***https://stackoverflow.com/questions/17110250/codedom-add-file-properties***](https://stackoverflow.com/questions/17110250/codedom-add-file-properties) I can not comment yet so i posted this as answer. – Speed Runer Nov 03 '21 at 21:50

0 Answers0