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:
//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");
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:
Assembly assembly = Assembly.LoadFile("PathToNewClassDLL");
Blah newBlah = assembly.CreateInstance("MyApp.BlahNamespace.Blah") as Blah;
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).
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?
Thanks