0

I know that one shouldn't include using namespace declarations in header files as they make items available to the whole project and that might have a bad result. But so far I have been unable to find info on whether a "#using" directive is allowed in header files. Can I use it in a header file like in this example?:

Here's my header file with function forward declaration:

#using <System.dll>

System::Security::Cryptography::X509Certificates::X509Certificate2^ GetCertificate(int Index);

If I can't, what is a workaroud for inclusion of System.dll, on which X509Certificate2 is dependant?

1 Answers1

0

The #using directive does something completely different from the using namespace keyword. It is the equivalent of the #include directive as used in native C++ code. It tells the compiler to load the type definitions from the assembly metadata.

And no, you should not be using it. It is pretty broken in recent VS versions, the exact version of the reference assemblies you use in your project matter a great deal since .NET 4.0. Lethal since 4.5 when you get the wrong one. Besides, System.dll is always referenced in a properly configured C++/CLI project so #using it again makes no sense.

Instead use Project + Properties, Common Properties, References. And be sure to use a project template from the CLR node when you start a new project.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Thank you for your answer, Hans. So if I'm using .NET 4.5, I should add those files as References and not even think about using _#using_, right? –  Mar 24 '15 at 17:37