473,770 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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. "GeneratedAssem bly.X.dll" where X is a new
number each time.

2. If I always use the same file ("GeneratedAsse mbly.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
"GeneratedAssem bly.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.Reflecti on;
using System.Threadin g;
using Microsoft.CShar p;

namespace Temp
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
AppDomain.Curre ntDomain.SetSha dowCopyFiles();
Class1 c1 = new Class1();
c1.DoStuff();
}

public void DoStuff()
{
for(int i=0;i<4;++i)
{
// THIS WORKS
Assembly a =
CreateAssembly( @".\Gen."+Guid. NewGuid().ToStr ing()+".dll");

// THIS DOESN'T WORK
//Assembly a =
CreateAssembly( @".\Gen.dll."+G uid.NewGuid().T oString());

// THIS DOESN'T WORK - THIS IS WHAT I NEED
//Assembly a = CreateAssembly( @".\Gen.dll" );

Type t = a.GetType("GenT emp.Gen");
MethodInfo mi = t.GetMethod("Pr int",
BindingFlags.St atic | BindingFlags.Pu blic);
mi.Invoke(null, new object[]{});
Thread.Sleep(10 00);
}
}

private Assembly CreateAssembly( string assemblyPath)
{
try
{
File.Delete(ass emblyPath);

string code = @"
using System;

namespace GenTemp
{
class Gen
{
public static void Print()
{
Console.WriteLi ne("""+Guid.New Guid().ToString ()+@""");
}
}
}
";
//Create an instance whichever code provider that is
needed
CodeDomProvider codeProvider = new
CSharpCodeProvi der();

//create the language specific code compiler
ICodeCompiler compiler =
codeProvider.Cr eateCompiler();

//add compiler parameters
CompilerParamet ers compilerParams = new
CompilerParamet ers();
compilerParams. CompilerOptions = @"/target:library" ;
// you can add /optimize
compilerParams. GenerateExecuta ble = false;
compilerParams. GenerateInMemor y = false;
compilerParams. OutputAssembly = assemblyPath;
compilerParams. IncludeDebugInf ormation = false;

// add some basic references
compilerParams. ReferencedAssem blies.Add("msco rlib.dll");
compilerParams. ReferencedAssem blies.Add("Syst em.dll");

//actually compile the code
CompilerResults results =
compiler.Compil eAssemblyFromSo urce(compilerPa rams,code);

if(results.Erro rs.HasErrors)
{
throw new
ApplicationExce ption(results.E rrors[0].ToString());
}
//get a hold of the actual assembly that was generated
Assembly generatedAssemb ly = results.Compile dAssembly;
//Assembly generatedAssemb ly =
Assembly.LoadFr om(assemblyPath );

//return the assembly
return generatedAssemb ly;
}
catch(Exception e)
{
throw new ApplicationExce ption("Couldn't compile:
"+e.Message , e);
}
}
}
}
Jul 21 '05 #1
0 1411

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

Similar topics

0
1891
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 24-28, 200) Organization committee: Roel Wuyts (primary contact - roel.wuyts@ulb.ac.be), Gilad Bracha, Wolfgang De Meuter, Stéphane Ducasse and Oscar Nierstrasz.
0
1345
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 any combination of textboxes, ddls, popup calendars, etc. The properties of the dynamic controls are stored in a SQL database so that my program can know what type of control to render and what items to put in the ddl etc. My problem is that I...
0
2073
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab, System and Software Engineering Lab), ULB (deComp) and the Belgian Association for Dynamic Languages (BADL) are very pleased to invite you to a whole day of presentations about the programming languages Self, Smalltalk and Common Lisp by experts in...
2
6359
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 3 ways to do this: 1. Write a script to create a semi-dynamic CSS file, which will be run whenever we need to make changes to the ASP variables controlling the
0
1216
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
7976
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 compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
0
1683
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 EASST. Note that we submission deadline has been extended. The important dates: - May 27: deadline for the workshop submissions. Submissions should follow LNCS format (www.springer.com/lncs) and should be sorter than
5
2585
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 algorithm to solve a certain messy problem, and many different things. For that I often use several general modules that I have written, like implementation of certain data structures, and small general "utility" functions/classes, plus of course...
0
9618
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10259
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.