473,387 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Dynamic re-compilation of assembly doesn't use new assembly

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);
}
}
}
}
Jul 21 '05 #1
0 1362

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Roel Wuyts | last post by:
CALL FOR CONTRIBUTIONS International Workshop on Revival of Dynamic Languages http://pico.vub.ac.be/~wdmeuter/RDL04/index.html (at OOPSLA2004, Vancouver, British Columbia, Canada, October...
0
by: pbb | last post by:
I have a web page on which I dynamically create controls based on the selection a user makes from a dropdownlist (this ddl is not dynamic). Depending on the user's selection, the controls could be...
0
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab,...
2
by: JWL | last post by:
Hi I need to create a bunch of sites with slightly dynamic CSS. Basically, all the image paths in the CSS need to be dynamic, depending on the values of certain ASP variables. I can think of...
0
by: alexandre.bergel | last post by:
Dear colleges, You might want to consider Dyla'07 as a good venue to present your work and your favourite programming language. Regards, Alexandre ...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
0
by: Alexandre Bergel | last post by:
Dear colleague, Please, note that after the workshop, best papers will be selected, and a second deadline will then be set regarding preparation of the Electronic Communications of the...
5
by: bearophileHUGS | last post by:
I often use Python to write small programs, in the range of 50-500 lines of code. For example to process some bioinformatics data, perform some data munging, to apply a randomized optimization...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.