473,581 Members | 2,491 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Referencing in-memory assembly on compile

I need compile in-memory assembly which references to other in-memory
assembly.

Compiling second assembly fails with error

Line: 0 - Metadata file 'eed7li9m, Version=0.0.0.0 , Culture=neutral ,
PublicKeyToken= null' could not be found

Saving assembly to Windows temp directory for referencing only creates huge
amout of temporary
files. I don't know any way to delete those files since they are locked by
loaded assemblies.
Creating separate Appdomain causes issues when I want to pass my
application types.

How to fix ?

Is is possible/reasonabe to compile assembly twice: one compile to memory
and second compile to temporary directory for reference only ?
Or is it possible to delete those temporary files ?

Andrus.
using System;
using System.CodeDom. Compiler;
using System.Windows. Forms;

class Program {
static void Main() {
Microsoft.CShar p.CSharpCodePro vider provider = new
Microsoft.CShar p.CSharpCodePro vider();

CompilerParamet ers compilerParamet ers = new CompilerParamet ers {
GenerateInMemor y = true
};

CompilerResults compilerResults =
provider.Compil eAssemblyFromSo urce(compilerPa rameters, "public
class Test {}");
if (compilerResult s.Errors.HasErr ors)
throw new ApplicationExce ption("Unexpect ed compile error");

var asm1 = compilerResults .CompiledAssemb ly;

compilerParamet ers.ReferencedA ssemblies.Add(a sm1.FullName);

compilerResults =
provider.Compil eAssemblyFromSo urce(compilerPa rameters, "public
class Test1 : Test {}");

if (compilerResult s.Errors.HasErr ors) {
string msg;
msg = compilerResults .Errors.Count.T oString() + " Errors:";
for (int x = 0; x < compilerResults .Errors.Count; x++)
msg = msg + "\r\nLine: " +
compilerResults .Errors[x].Line.ToString( ) + " - " +
compilerResults .Errors[x].ErrorText;

//Causes Line: 0 - Metadata file 'eed7li9m, Version=0.0.0.0 ,
Culture=neutral ,
//PublicKeyToken= null' could not be found
MessageBox.Show (msg);
}
}
}

Jun 27 '08 #1
2 6911
You need to use reflection to read the public API of the assembly that you
want to reference, then generate code for skeleton for all the public
classes and it's members, also add an static initializer that takes in an
Assembly object reference and saves it to a static field. Then add two
fields, one of type System.Object called _instance, and one of type
System.Type called _instanceType to all your generated classes and on the
constructors use the static Assembly object's CreateInstance to create an
instance of the type on the referenced assembly and save it to the
_instanceField. Then use the GetTypes() method to get an array of the types
that the referenced assembly contains, iterate through it comparing the
..FullName property with the full name of the type you're referencing and
when you find it save it to the _instanceType field. Now on you generated
methods you can use the Invoke method of the _instanceType field to call
methods on your remote class. Then you need to add this generated code to
the code that you're trying to compile. Additionally you need to add one
more static initializer method to the code being compiled, it should take in
the Assembly object reference and pass it to the static initializer of the
generated code. Finally after you compile the assembly and load it you must
call the static initializer pass the "reference to the referenced assembly"
to it.

It's not as hard as it sounds since reflection will give you all the info
that you need to generate the code tree.

Hope that helps,
Fernando L Rodriguez, MCP

"Andrus" <ko********@hot .eewrote in message
news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
>I need compile in-memory assembly which references to other in-memory
assembly.

Compiling second assembly fails with error

Line: 0 - Metadata file 'eed7li9m, Version=0.0.0.0 , Culture=neutral ,
PublicKeyToken= null' could not be found

Saving assembly to Windows temp directory for referencing only creates
huge
amout of temporary
files. I don't know any way to delete those files since they are locked by
loaded assemblies.
Creating separate Appdomain causes issues when I want to pass my
application types.

How to fix ?

Is is possible/reasonabe to compile assembly twice: one compile to memory
and second compile to temporary directory for reference only ?
Or is it possible to delete those temporary files ?

Andrus.
using System;
using System.CodeDom. Compiler;
using System.Windows. Forms;

class Program {
static void Main() {
Microsoft.CShar p.CSharpCodePro vider provider = new
Microsoft.CShar p.CSharpCodePro vider();

CompilerParamet ers compilerParamet ers = new CompilerParamet ers {
GenerateInMemor y = true
};

CompilerResults compilerResults =
provider.Compil eAssemblyFromSo urce(compilerPa rameters, "public
class Test {}");
if (compilerResult s.Errors.HasErr ors)
throw new ApplicationExce ption("Unexpect ed compile error");

var asm1 = compilerResults .CompiledAssemb ly;

compilerParamet ers.ReferencedA ssemblies.Add(a sm1.FullName);

compilerResults =
provider.Compil eAssemblyFromSo urce(compilerPa rameters, "public
class Test1 : Test {}");

if (compilerResult s.Errors.HasErr ors) {
string msg;
msg = compilerResults .Errors.Count.T oString() + " Errors:";
for (int x = 0; x < compilerResults .Errors.Count; x++)
msg = msg + "\r\nLine: " +
compilerResults .Errors[x].Line.ToString( ) + " - " +
compilerResults .Errors[x].ErrorText;

//Causes Line: 0 - Metadata file 'eed7li9m, Version=0.0.0.0 ,
Culture=neutral ,
//PublicKeyToken= null' could not be found
MessageBox.Show (msg);
}
}
}

Jun 27 '08 #2
Fernando,
You need to use reflection to read the public API of the assembly that you
want to reference, then generate code for skeleton for all the public
classes and it's members, also add an static initializer that takes in an
Assembly object reference and saves it to a static field. Then add two
fields, one of type System.Object called _instance, and one of type
System.Type called _instanceType to all your generated classes and on the
constructors use the static Assembly object's CreateInstance to create an
instance of the type on the referenced assembly and save it to the
_instanceField. Then use the GetTypes() method to get an array of the
types that the referenced assembly contains, iterate through it comparing
the .FullName property with the full name of the type you're referencing
and when you find it save it to the _instanceType field. Now on you
generated methods you can use the Invoke method of the _instanceType field
to call methods on your remote class. Then you need to add this generated
code to the code that you're trying to compile. Additionally you need to
add one more static initializer method to the code being compiled, it
should take in the Assembly object reference and pass it to the static
initializer of the generated code. Finally after you compile the assembly
and load it you must call the static initializer pass the "reference to
the referenced assembly" to it.

It's not as hard as it sounds since reflection will give you all the info
that you need to generate the code tree.
Thank you. This looks complicated for me.
Where to find generic code which implements this ?
Currently I implemented code which creates assemblies in windows temp
directory.
In next incovation my application clean temp directory from the assemblies
which are created during previous run.
Is this OK ?
Unfortunately MS Reportviewer control in local mode cannot still find any
dynamic assembly.

Andrus.
Jun 27 '08 #3

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

Similar topics

4
1660
by: Bosconian | last post by:
I've been researching this for hours and can't seem to find the right syntax. I need to retrieve a value of an array by referencing the element using a string variable. For example: $data = 'hank'; $element = 'lastname';
2
8041
by: Simon | last post by:
Am using the following code. <script language="JavaScript1.2"> function setquantity(productindex,productquantity) { //create the reference object irefname_none = eval("document." + productindex + "none"); <snip>
5
2584
by: Tony Williams | last post by:
I have a form on which there is a tabcontrol and on this onr of the tabpages is a tick box which opens Outlook with data from the main form and prompts the user to amend the message before being sent. The code is Private Sub Loadtxt_Click() Dim oA As Outlook.Application Dim oM As Outlook.MailItem Set oA =...
2
3808
by: Paul Cook | last post by:
Hi, I have three tables: Countries: ID Country States: ID
3
1944
by: Lyners | last post by:
I have a table within a cell of a datagrid. I am doing updates without postback to the server using Javascript. I have everything working, except referencing a table within the datagrid cell. Here is my datagrid code for the cell; <asp:TemplateColumn HeaderText="3Fields" ItemStyle-Font-Size="8" SortExpression="Field1 ASC"...
2
1143
by: Terry | last post by:
What is the syntax for referencing the properties of objects within Collections of objects? EXAMPLE: Dim MCoil As New Coil MCoil = CoilCollection("3456") TextBox1.Text() = MCoil.CoilDesc WANT TO BE ABLE TO AVOID INTERMEDIARY COPY (SOEMTHING
3
1053
by: markh | last post by:
Does anyone know where i could find a good website that explains many techniques for direct control referencing? thanks markh
9
1585
by: Brett Romero | last post by:
Say I have a library (A.dll) with a method that accepts a collection of a specific type. The type is defined in B.dll. In A.dll, I need to loop through this collection and reference fields of the type. For example, a Person type. It may have arm, leg, feet, and hands. Hands is a collection containing types of fingers. The only way to...
1
1985
by: vojinle | last post by:
Hi, Is there any example populating TreeView control from self-referencing database table? Table structure: ID Item_name Parent_ID
0
7857
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...
0
8299
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8162
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3797
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3811
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2297
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
1
1397
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1125
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...

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.