473,386 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to add an assembly reference using CodeDOM?

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);
Jul 21 '05 #1
3 9375
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

Mountain Bikn' Guy wrote:
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);


Jul 21 '05 #2
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" <sh*********@yahoo.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
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

Mountain Bikn' Guy wrote:
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);

Jul 21 '05 #3
"Mountain Bikn' Guy" <vc@attbi.com> wrote in message
news:gsdlb.845178$uu5.150092@sccrnsc04...
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
Jul 21 '05 #4

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

Similar topics

1
by: Vladimir Semenov | last post by:
Hi, I'm trying to serialize a type contained in assembly referering to another assembly in GAC. XmlSerializer xs = new XmlSerializer( typeof(TransferParentData ) ); The code above throws...
3
by: Mountain Bikn' Guy | last post by:
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 ...
2
by: Cornu Nicolas | last post by:
Hello, The above code generate the above error. The not found assembly is different at each execution. -------------------------------------------------------------------------- public void...
0
by: Martinh | last post by:
Hi After invoking RetrieveData in a WebService I recieve the following response in a new window... I am running Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573 ...
0
by: jcvoon | last post by:
Hi: My application will compile a source code and generate an executable assembly during run time, i'm able to generate the exe, but i don't know the correct way to link a resource file into...
0
by: Richard Gregory | last post by:
Hi, I have the wsdl below, for an Axis web service, and when I select Add Web Refernce in Visual Studio the proxy is missing a class representing the returnedElementsType (see reference.cs below...
2
by: Luis Arvayo | last post by:
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:...
2
by: Andrus | last post by:
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,...
4
by: Montezuma's Daughter | last post by:
Hi all it is my first application. I wrote a class libraray with functions and a simple class with form, textbox, buttons and a label. I added reference to the second class so I would read the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.