user34537
user34537

Reputation:

Add reference in code with C#?

I am looking for the equivalent of C++'s #pragma comment(lib, "name"); but for C# and adding assembly references. How can i do it?

Upvotes: 1

Views: 1542

Answers (4)

Jose Basilio
Jose Basilio

Reputation: 51548

If I understand correctly, you can use PInvoke through [DLLImport] as in the example below:

[DllImport("User32.dll", SetLastError=true)]
static extern Boolean MessageBeep(UInt32 beepType);

Upvotes: 1

Ana Betts
Ana Betts

Reputation: 74702

You can't do this, because the C# compiler needs the reference beforehand to be able to generate the IL, whereas the C++ compiler has header files that describe the layout of the referenced libraries (at least from a parser/verification perspective - it needs the LIB in the generation phase to actually be able to write the offsets)

Upvotes: 0

Gishu
Gishu

Reputation: 136673

Don't think you can dynamically add a reference via code.

However you could load the needed assembly via Reflection in code (Assembly.LoadXXX methods) and then access the types defined in it.

Upvotes: 2

Mitch Wheat
Mitch Wheat

Reputation: 300797

Please see:

Calling a C++ lib from C#

Call Unmanaged DLLs from C#

Upvotes: 3

Related Questions