0

How can one create a .dll dynamically from code out of several classes?

Creation-example:

 var test = CodeDomProvider
   .CreateProvider("CSharp")
   .CompileAssemblyFromSource(parameters, "public class MyClass { ... }");

CompileAssemblyFromSource expects a string (representation of a class).

I do not want to provide every class which shall be in the generated assembly as a string, so I want to pass them on creation.

Example:

  public class MyClass
  {
    [DataMember(Order = 1)]
    public bool Valid{ get; set; }

    [DataMember(Order = 1)]
    public string SomeText{ get; set; }
  }

Is there something like

var test = CodeDomProvider
  .CreateProvider("CSharp")
  .CompileAssemblyFromSource(parameters, MyClass);
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Arno Nym
  • 49
  • 1
  • 7

1 Answers1

0

Solution:

add "ReferencedAssemblies" to the parameters passed as first argument to CompileAssemblyFromSource.

var parameters = new CompilerParameters
      {
        GenerateExecutable = false,
        OutputAssembly = MY_ASSEMBLY,
        ReferencedAssemblies = {"System.Core.dll", "System.ServiceModel.dll"}
      };
Arno Nym
  • 49
  • 1
  • 7