How to define the path for runtime compiled assembly

L

Luis Arvayo

I am compiling and executing c# code at runtime and I need to define in
CompilerParameters.ReferencedAssemblies one of my own assemblies together
with the standard System.dll u others.

Example:

// compiler options
System.CodeDom.Compiler.CompilerParameters options = new
System.CodeDom.Compiler.CompilerParameters();
options.GenerateExecutable = false; // result is a dll
options.GenerateInMemory = true;
//options.CompilerOptions = "/target:library";
options.MainClass = "MyClass";
options.ReferencedAssemblies.AddRange(new string[]{"System.dll",
"System.Data.dll",
System.Windows.Forms.Application.StartupPath + @"\MyAssembly.dll"});
********
options.IncludeDebugInformation = false;

// create the compilker
ICodeCompiler compiler = (new
Microsoft.CSharp.CSharpCodeProvider()).CreateCompiler();

// build and look for compiler errors
System.CodeDom.Compiler.CompilerResults result =
compiler.CompileAssemblyFromSource(options, source.ToString());



From the above code, on the line highlighted (*) if I don't define the
Application.StartupPath as the directory where to found my own assembly, the
"File not found" exception is thrown.

But that fact forces me to always define the location of the assembly as
same as the application startup path (where the main .exe resides).

The above code belongs also to other assembly used in applications and not
the application per se. So, that assembly is tied to reference the other
assembly used, with that path.

Then the question is: how can I do in order to avoid the path dependency of
the referenced assembly as described above ?

Thanks in advance.

Luis Arvayo
 
G

Greg Young

That code is choosing to use applicationStartupPath

System.Windows.Forms.Application.StartupPath + @"\MyAssembly.dll"

You could use whatever path you would like there? Pass in a path from the
code calling your method .. use the executing assembly to get its path?
There are a score of ways of doing this.

I guess I am not following the question.

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung
 
L

Luis Arvayo

Hello,

Thanks for your answer.

I hope I can explain more clearly.

I have two assemblies. The assembly (we can call it assembly 1) that
compiles code at runtime and that depends on "MyAssembly.dll" (this is
assembly 2).

If I define the code inside assembly 1 like this:

options.ReferencedAssemblies.AddRange(new string[]{"System.dll",
"System.Data.dll", "MyAssembly.dll"});


Then a "File not found" for the "MyAssembly.dll" (or assembly 2) is raised.
that is to say. NET cannot find assembly 2.

But if I define this way:


Code inside assembly 1 :

options.ReferencedAssemblies.AddRange(new string[]{"System.dll",
"System.Data.dll",
System.Windows.Forms.Application.StartupPath + @"\MyAssembly.dll"});


Then no error is raised.

But I don't want to tell to the developer/customer:

"You must place assembly 2 on the Application.Startup subdirectory".

Is there a more general solution in order to that .NET can find assembly 2
withouth defining the full path ?


That is to say, I need to define it in a more general solution. But the
question is how to do that ?


Thanks.



Greg Young said:
That code is choosing to use applicationStartupPath

System.Windows.Forms.Application.StartupPath + @"\MyAssembly.dll"

You could use whatever path you would like there? Pass in a path from the
code calling your method .. use the executing assembly to get its path?
There are a score of ways of doing this.

I guess I am not following the question.

Cheers,

Greg Young
MVP - C#
http://geekswithblogs.net/gyoung
Luis Arvayo said:
I am compiling and executing c# code at runtime and I need to define in
CompilerParameters.ReferencedAssemblies one of my own assemblies together
with the standard System.dll u others.

Example:

// compiler options
System.CodeDom.Compiler.CompilerParameters options = new
System.CodeDom.Compiler.CompilerParameters();
options.GenerateExecutable = false; // result is a dll
options.GenerateInMemory = true;
//options.CompilerOptions = "/target:library";
options.MainClass = "MyClass";
options.ReferencedAssemblies.AddRange(new string[]{"System.dll",
"System.Data.dll",
System.Windows.Forms.Application.StartupPath + @"\MyAssembly.dll"});
********
options.IncludeDebugInformation = false;

// create the compilker
ICodeCompiler compiler = (new
Microsoft.CSharp.CSharpCodeProvider()).CreateCompiler();

// build and look for compiler errors
System.CodeDom.Compiler.CompilerResults result =
compiler.CompileAssemblyFromSource(options, source.ToString());



From the above code, on the line highlighted (*) if I don't define the
Application.StartupPath as the directory where to found my own assembly,
the "File not found" exception is thrown.

But that fact forces me to always define the location of the assembly as
same as the application startup path (where the main .exe resides).

The above code belongs also to other assembly used in applications and
not the application per se. So, that assembly is tied to reference the
other assembly used, with that path.

Then the question is: how can I do in order to avoid the path dependency
of the referenced assembly as described above ?

Thanks in advance.

Luis Arvayo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top