Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate Instance from assembly and its dependencies in C#
    primarykey
    data
    text
    <p>I have an app (let's just call it MyApp) that dynamically creates source code for a class and then compiles it. When it compiles the source code I also reference another DLL (that is the base class for this newly created class) that already exists in another folder. I do the following to compile and output the DLL:</p> <pre><code>//Create a C# code provider CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp"); //Set the complier parameters CompilerParameters cp = new CompilerParameters(); cp.GenerateExecutable = false; cp.GenerateInMemory = false; cp.TreatWarningsAsErrors = false; cp.WarningLevel = 3; cp.OutputAssembly = "SomeOutputPathForDLL"; // Include referenced assemblies cp.ReferencedAssemblies.Add("mscorlib.dll"); cp.ReferencedAssemblies.Add("System.dll"); cp.ReferencedAssemblies.Add("System.Core.dll"); cp.ReferencedAssemblies.Add("System.Data.dll"); cp.ReferencedAssemblies.Add("System.Data.DataSetExtensions.dll"); cp.ReferencedAssemblies.Add("System.Xml.dll"); cp.ReferencedAssemblies.Add("System.Xml.Linq.dll"); cp.ReferencedAssemblies.Add("MyApp.exe"); cp.ReferencedAssemblies.Add("SomeFolder\SomeAdditionalReferencedDLL.dll"); // Set the compiler options cp.CompilerOptions = "/target:library /optimize"; CompilerResults cr = provider.CompileAssemblyFromFile(cp, "PathToSourceCodeFile"); </code></pre> <p>Later on in my app (or next time the app runs) I try to create an instance of the class. I know where both the DLL for the newly created class (let's call it Blah) and the base class is. I use the following code to try to create an instance of the new class:</p> <pre><code>Assembly assembly = Assembly.LoadFile("PathToNewClassDLL"); Blah newBlah = assembly.CreateInstance("MyApp.BlahNamespace.Blah") as Blah; </code></pre> <p>When I call Assembly.CreateInstance like I do above I get an error saying it cannot create the instance. When I check assembly.GetReferencedAssemblies() it has the standard references and the reference for my app (MyApp.exe) but it doesn't have the reference for the dependent base class that I used when compiling the class originally (SomeAdditionalReferencedDLL.dll).</p> <p>I know that I have to add the base class reference somehow in order to create the instance but I am not sure how to do this. How do I create an instance of a class from an assembly when I have the assembly and all of it dependecies?</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload