I'm trying to dynamically compile assemblies and cache them to disk,
which seems to work fine. When the data I'm compiling from changes, I
want to re-generate the assembly and use the new version.
After I re-generate the assembly, I get the type I want from it and
then invoke a static method.
I have found the following behaviour:
1. If I always regenerate the assembly with a completely new name,
then it works fine... e.g. "GeneratedAssembly.X.dll" where X is a new
number each time.
2. If I always use the same file ("GeneratedAssembly.dll"), which is
what I really want to do, then it always invokes the method in the
first assembly I generate, and ignores new assemblies.
3. If I change the format of the name to something like
"GeneratedAssembly.dll.X", i.e. like case 1 but with the DLL extension
not at the end, then it always invokes the method in the first
assembly I generate like case 2.
So there seems to be something in the way .NET checks for loaded DLLs
that is causing my new DLLs not to be reloaded... can anyone help me
find a solution so I can regenerate over the top of the origional DLL
and have it load the new version?
I know you can't unload DLLs unless they're in a different AppDomain,
but I have lots of these small DLLs being generated continuously and
can't afford to put each in a different AppDomain. What I will do is
once I have too many 'dead' assemblies in memory, I will reload the
whole AppDomain in one go. So I really need my new assemblies to be
reloaded and used in the same AppDomain as the assembly it's
replacing.
Any help will be welcome.
Here is some quick sample code which shows the problem (the DoStuff
method has the three cases to try - just uncomment the one you want to
see).
Thanks,
James.
using System;
using System.CodeDom.Compiler;
using System.IO;
using System.Reflection;
using System.Threading;
using Microsoft.CSharp;
namespace Temp
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
AppDomain.CurrentDomain.SetShadowCopyFiles();
Class1 c1 = new Class1();
c1.DoStuff();
}
public void DoStuff()
{
for(int i=0;i<4;++i)
{
// THIS WORKS
Assembly a =
CreateAssembly(@".\Gen."+Guid.NewGuid().ToString() +".dll");
// THIS DOESN'T WORK
//Assembly a =
CreateAssembly(@".\Gen.dll."+Guid.NewGuid().ToStri ng());
// THIS DOESN'T WORK - THIS IS WHAT I NEED
//Assembly a = CreateAssembly(@".\Gen.dll");
Type t = a.GetType("GenTemp.Gen");
MethodInfo mi = t.GetMethod("Print",
BindingFlags.Static | BindingFlags.Public);
mi.Invoke(null,new object[]{});
Thread.Sleep(1000);
}
}
private Assembly CreateAssembly(string assemblyPath)
{
try
{
File.Delete(assemblyPath);
string code = @"
using System;
namespace GenTemp
{
class Gen
{
public static void Print()
{
Console.WriteLine("""+Guid.NewGuid().ToString()+@" "");
}
}
}
";
//Create an instance whichever code provider that is
needed
CodeDomProvider codeProvider = new
CSharpCodeProvider();
//create the language specific code compiler
ICodeCompiler compiler =
codeProvider.CreateCompiler();
//add compiler parameters
CompilerParameters compilerParams = new
CompilerParameters();
compilerParams.CompilerOptions = @"/target:library";
// you can add /optimize
compilerParams.GenerateExecutable = false;
compilerParams.GenerateInMemory = false;
compilerParams.OutputAssembly = assemblyPath;
compilerParams.IncludeDebugInformation = false;
// add some basic references
compilerParams.ReferencedAssemblies.Add("mscorlib. dll");
compilerParams.ReferencedAssemblies.Add("System.dl l");
//actually compile the code
CompilerResults results =
compiler.CompileAssemblyFromSource(compilerParams, code);
if(results.Errors.HasErrors)
{
throw new
ApplicationException(results.Errors[0].ToString());
}
//get a hold of the actual assembly that was generated
Assembly generatedAssembly = results.CompiledAssembly;
//Assembly generatedAssembly =
Assembly.LoadFrom(assemblyPath);
//return the assembly
return generatedAssembly;
}
catch(Exception e)
{
throw new ApplicationException("Couldn't compile:
"+e.Message, e);
}
}
}
}