I believe this does everything you need it to excuse the sloppy code ... I
will put up a blog post later today about it and post link here.
using System;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.Reflection;
namespace ConsoleApplication9
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
class Program {
static void GenerateMainAssembly() {
Microsoft.CSharp.CSharpCodeProvider codeProvider = new
Microsoft.CSharp.CSharpCodeProvider();
ICodeCompiler compiler = codeProvider.CreateCompiler();
CompilerParameters parameters = new CompilerParameters(new string[]
{"mscorlib.dll"});
parameters.ReferencedAssemblies.Add("System.dll");
parameters.ReferencedAssemblies.Add("inmemoryassem bly.dll");
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;
string source = "";
source += "public class MyObject {";
source += " public void Test() {";
source += " System.Console.WriteLine(\"Test\");";
source += " OtherObject.SomeMethod();";
source += " System.Console.ReadLine();";
source += " }";
source += "}";
CompilerResults results = compiler.CompileAssemblyFromSource(parameters,
source);
//call test
object o = results.CompiledAssembly.CreateInstance("MyObject" );
Type t = o.GetType();
MethodInfo mi = t.GetMethod("Test");
mi.Invoke(o, null);
}
private static void CheckResults(CompilerResults Results) {
if(Results.Errors.Count > 0) {
throw new System.Exception("Compile Failed");
}
}
static void GenerateOtherAssembly() {
CSharpCodeProvider codeProvider = new
Microsoft.CSharp.CSharpCodeProvider();
ICodeCompiler compiler = codeProvider.CreateCompiler();
CompilerParameters parameters = new CompilerParameters(new string[]
{"mscorlib.dll"});
parameters.ReferencedAssemblies.Add("System.dll");
parameters.OutputAssembly = "inmemoryassembly.dll";
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;
string source = "";
source += "public class OtherObject {";
source += " public static void SomeMethod() {";
source += " System.Console.WriteLine(\"foo\");";
source += " }";
source += "}";
CompilerResults results = compiler.CompileAssemblyFromSource(parameters,
source);
CheckResults(results);
}
static void Main(string[] args) {
GenerateOtherAssembly();
GenerateMainAssembly();
}
}
}
}
"Jan" <j0****************@world.dk> wrote in message
news:22**********************************@microsof t.com...
I'm using the CSharpCodeProvider to buils some assemblies @ runtime which
are
never saved as files (cp.GenerateInMemory = true;).
The generated assemblies are hierachically dependent on each other so I
generate the "bottom" assemblies first.
How do I add a dependency to another previously loaded (generated)
assembly?
I would be happy if CompilerParameters.ReferencedAssemblies.Add could take
a
System.Reflection.Assembly reference as parameter.
A possible solution would be to generate dll files and reference them but
I
like the idea of not having any files to cleanup when my application
exits.
Any suggestions appreciated.
/Jan