1

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?

1
  • Do you want to reference a native library or another .NET assembly? Commented Jun 1, 2009 at 8:11

4 Answers 4

3

Please see:

Calling a C++ lib from C#

Call Unmanaged DLLs from C#

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

1

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

Comments

0

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)

Comments