Re: Referencing in-memory assembly on compile



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" <kobruleht2@xxxxxx> wrote in message
news:%230OC8DHrIHA.3940@xxxxxxxxxxxxxxxxxxxxxxx
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.CSharp.CSharpCodeProvider provider = new
Microsoft.CSharp.CSharpCodeProvider();

CompilerParameters compilerParameters = new CompilerParameters {
GenerateInMemory = true
};

CompilerResults compilerResults =
provider.CompileAssemblyFromSource(compilerParameters, "public
class Test {}");
if (compilerResults.Errors.HasErrors)
throw new ApplicationException("Unexpected compile error");

var asm1 = compilerResults.CompiledAssembly;

compilerParameters.ReferencedAssemblies.Add(asm1.FullName);

compilerResults =
provider.CompileAssemblyFromSource(compilerParameters, "public
class Test1 : Test {}");

if (compilerResults.Errors.HasErrors) {
string msg;
msg = compilerResults.Errors.Count.ToString() + " 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);
}
}
}





.



Relevant Pages

  • Re: Where is System.Collections when using csc.exe
    ... Copyright Microsoft Corporation 2001-2005. ... You should not reference assemblies in the GAC, ... can compile my code without errors. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Command line devenv not picking up dependencies on managed p
    ... commandline and it does not copy any of the C# reference assemblies to ... the build log shows that the xcopy command is incorrect. ... >> already been copied to the output folder by the manual compile. ...
    (microsoft.public.vsnet.ide)
  • Re: Define assembly folder?
    ... If you have a project that you want to compile, ... > I'm aware of reference paths. ... > Maybe I was a bit unclear, but I'm wondering how to set reference paths ... I have my assemblies in different locations and I don't want ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Referencing in-memory assembly on compile
    ... Assembly object reference and saves it to a static field. ... System.Type called _instanceType to all your generated classes and on the ... add one more static initializer method to the code being compiled, ... In next incovation my application clean temp directory from the assemblies ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Creating a custom class/Namespace
    ... >> How then would I reference this in a new Web Application ... To compile a single source code file, ... If you need to reference a .Net Framework assembly or assemblies, ... I get the message in the compiler output that my namespace ...
    (microsoft.public.dotnet.framework.aspnet)