In C#, there are three types of using directives:
using System; // Specify Namespace
using Diag = System.Diagnostics; // Specify Namespace Alias
using DBG = System.Diagnostics.Debug; // Specify Class Alias
In C++/CLI, I know the equivalents to the first two:
using namespace System;
namespace Diag = System::Diagnostics;
Is there any way to do the third one in C++/CLI?
Doing namespace DBG = System::Diagnostics::Debug;
gives error C2879: 'System::Diagnostics::Debug' : only an existing namespace can be given an alternative name by a namespace alias definition
The only alterntive I've come up with is #define DBG System::Diagnostics::Debug
, but I'd prefer a proper using directive, if available.
A namespace can be given an alias (i.e., another name for the same namespace) using the namespace identifier = syntax. Members of the aliased namespace can be accessed by qualifying them with the name of the alias.
Fortunately, C# provides a way to alias a long namespace or class name while retaining full type-checking. To alias a namespace or class name, use the using directive to define the alias as shown in the sample code below. Then you can use the alias anywhere you would normally use the class name or namespace.
Type aliasing is a little known feature for C# and it comes in handy when you would like to alias a specific method or class from a namespace for the sake of clarity. At the heart of these features is the using keyword.
You can also create an alias for a namespace or a type with a using alias directive.
A C++ typedef will do the trick here.
typedef System::Diagnostics::Debug DBG;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With