views:

163

answers:

7

In msvc i can write

#pragma comment(lib, "my.lib");

which includes my.lib in the linking stage. In my solution i have 2 projects. One is a class project the other is my main. How do i include the reference dll in code instead of adding the reference in the project?

+3  A: 

As far as I know, you can't. If you need to access type that are included in a non referenced assembly, you'll have to use Assembly.Load().

Fabian Vilers
+1  A: 

I don't think you can include a dll from code without adding a reference. What you can do however is to use reflection to load that assembly and use a type from that assembly.

Assembly.Load() will get you a handle on the assembly and then you should be able to iterate through the types in the assembly.

Jaco Pretorius
Yes, use reflection at run time to inspect the assembly and reflect on packages needed to run the assembly. I echo the above the comment = )
IbrarMumtaz
+2  A: 

I'm afraid you can't.

You can dynamically load the assembly via Assembly.Load(...) but then you have use reflection to explicitly create each Type you need to use.

cedrou
+4  A: 

Contrary to popular belief, it is possible :-)

To statically link .NET assemblies check out ILMerge. It's a utility that can be used to merge multiple .NET assemblies into a single one. Be it an executable or a DLL.

You could create a batch script that packages your assemblies together as a post-build step.

Edit: One thing to note however is that this does not remove the need to reference the library. The reference is still needed in order to compile your code that is dependent the external types. Much like including header files under C(++). By default c# assemblies are independent, there is no linking involved. However the tool I mentioned above allows you to create a new assembly with the external dependencies included.

Yannick M.
The question was about adding a reference using a compiler preprocessing instruction in code.
Fabian Vilers
The `#pragma comment(...)` directive puts a comment on an object file, which is then read by the linker in order to correctly link its dependencies. It is by no means actual code, it is just a marker for the linker. So I am assuming that his question was about statically linking assemblies.
Yannick M.
A: 

If you want to load an assembly at runtime, you can use Assembly.LoadFrom(filePath). But that way you are not referencing the assembly, and you can't use strong typing.

For example, you can have different plugins implementing a known interface (the one which is in a separate, referenced assembly), and have them all placed in a folder. Then you can check the folder and load all implementing classes at runtime (like in this example).

Groo
Assembly.LoadFrom(filePath)has some disadvantages. Microsoft (and I) recommends to consider using Assembly.Load() instead
ironic
Does Microsoft (and you) have some links which would explain the disadvantages? And is it possible to completely replace `LoadFrom` functionality with `Load`?
Groo
Here you can read about it.http://msdn.microsoft.com/en-us/library/1009fa28.aspx <br>As far as this function is not removed or marked deprecated, I think there are cases where it IS necessary and cannot be replaced with Load(). But in my experience, not reading about disadvantages lead to some problems with dll loading, so after about an hour I understood that I should have used Load(). I am too lazy to recall disadvantages namely, so I just remind that they exist. The rest is up to reader.
ironic
+1  A: 

Managed code doesn't use a linker. The C/C++ equivalent of a reference assembly is the #include directive, you need that in C/C++ to allow the compiler to generate code for an external type. Exact same thing in C#, you can't use an external type unless the compiler has a definition for it. The reference assembly supplies that.

The equivalent of C/C++ linking is done at runtime in a managed program. The JIT compiler loads assemblies as needed to generate machine code.

One thing you can do in a C# program that you can't do in a C/C++ program is using Reflection. It allows you to invoke a constructor and call a type's methods with type and method names as strings. Start that ball rolling with Assembly.GetType() and the methods of the Type class. However, consider a plug-in model with, say, the System.AddIn namespace first.

Hans Passant
A: 

In addition to use reflection there is one more method to instantiate dll i.e. Codedom

Even you can create dlls at runtime very easily.

Just check below articles

Codedom 1

C# on the fly

Harryboy