How to add an assembly reference using CodeDOM?

M

Mountain Bikn' Guy

This code (adapted from the examples in the docs) doesn't make complete
sense to me. I have it working, but I'm wondering why I need to declare an
assembly reference in 2 places. TIA.
Dave

CodeCompileUnit compileUnit = new CodeCompileUnit();
CodeNamespace myNamespace = new CodeNamespace("MyNamespace");
compileUnit.Namespaces.Add( myNamespace );
compileUnit.ReferencedAssemblies.Add(@"MyDLL.dll");//I declare an assembly
reference here first


myNamespace.Imports.Add( new CodeNamespaceImport("System") );
myNamespace.Imports.Add( new CodeNamespaceImport("MyOtherNamespace") );

[...]

ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters cp = new CompilerParameters(
new string[] {"System.dll",

"MyDLL.dll" //Now I still have to declare the assembly reference here too.
Why?

},

filepath.Substring(0, filepath.LastIndexOf(".") + 1)+"dll", false);
 
K

Kalpesh

I think this is similar to VS.NET IDE does internally

You must be knowing that
1) You can add references to assemblies that you wish to use by "Add
Reference"
2) You can use any of the classes of "References" inside your classes
using "using" statement

So, while compiling, you need to provide, which DLLs you want to add to
current assembly in compilation.

Since, there is a possibility that
Although you have added a ref. to an external assembly, but you dont
want to it to be included as a part of your assembly

Correct me, anyone, if I am conceptually wrong

Kalpesh
 
M

Mountain Bikn' Guy

The "using" statements are taken care of by CodeNamespaceImport. However,
I'm speaking specifically about the need to declare assembly references
twice. I make one call with ReferencedAssemblies.Add. Then I am still
required (or I get errors) to define the assembly reference again as a
CompilerParameters parameter. That is the part that doesn't make sense to
me. It seems that ReferencedAssemblies.Add is not fully taking care of of
the task of defining a referenced assembly.
Dave



Kalpesh said:
I think this is similar to VS.NET IDE does internally

You must be knowing that
1) You can add references to assemblies that you wish to use by "Add
Reference"
2) You can use any of the classes of "References" inside your classes
using "using" statement

So, while compiling, you need to provide, which DLLs you want to add to
current assembly in compilation.

Since, there is a possibility that
Although you have added a ref. to an external assembly, but you dont
want to it to be included as a part of your assembly

Correct me, anyone, if I am conceptually wrong

Kalpesh
This code (adapted from the examples in the docs) doesn't make complete
sense to me. I have it working, but I'm wondering why I need to declare an
assembly reference in 2 places. TIA.
Dave

CodeCompileUnit compileUnit = new CodeCompileUnit();
CodeNamespace myNamespace = new CodeNamespace("MyNamespace");
compileUnit.Namespaces.Add( myNamespace );
compileUnit.ReferencedAssemblies.Add(@"MyDLL.dll");//I declare an assembly
reference here first


myNamespace.Imports.Add( new CodeNamespaceImport("System") );
myNamespace.Imports.Add( new CodeNamespaceImport("MyOtherNamespace") );

[...]

ICodeCompiler compiler = provider.CreateCompiler();
CompilerParameters cp = new CompilerParameters(
new string[] {"System.dll",

"MyDLL.dll" //Now I still have to declare the assembly reference here too.
Why?

},

filepath.Substring(0, filepath.LastIndexOf(".") + 1)+"dll", false);
 
A

Alan Pretre

Mountain Bikn' Guy said:
This code (adapted from the examples in the docs) doesn't make complete
sense to me. I have it working, but I'm wondering why I need to declare an
assembly reference in 2 places.

CodeCompileUnit compileUnit = new CodeCompileUnit();

Hi. I'm wondering why you are working with CodeCompileUnit at all? Do you
have source code you are trying to compile or what?

If you have source code to work with then you would create the Compiler
object, add your CompilerParameters object, add your ReferencedAssemblies
and call CompileAssemblyFromSource() to compiler. In this situation there
is only one time you need to add references.

-- Alan
 

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