简体   繁体   中英

include assemblies via code? C#

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?

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.

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() .

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.

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.

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.

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM