John Weisz
John Weisz

Reputation: 31924

How do I add a shorter alias to a namespace used by another project?

Good afternoon!

I have two C# projects in a Visual Studio solution with the following namespace structure (reflects directory layout):

MySolution
|-- MyLibrary
|   |-- App_Code
|   |   |-- Entities
|   |   |   `-- ...
|   |   `-- ...
|   `-- MainClass.cs
`-- MyApplication
    |-- App_Code
    |   `-- ...
    `-- Main.cs

In the MyApplication project I'd like to use a few classes from the Entities namespace of the MyLibrary project without having to specify its fully qualified name each time:

...
// using MyLibrary.App_Code.Entities; // too long and ugly
using MyLibrary.Entities; // much better

namespace MyApplication
{
    class Main
    {
        ...
    }
}

How can I define MyLibrary.Entities as an alias to MyLibrary.App_Code.Entities inside MyLibrary (and thus avoiding the need to do it manually each time a component is used)?

Upvotes: 3

Views: 504

Answers (1)

Eulogy
Eulogy

Reputation: 317

Option 1: Name the namespace of Entities classes (MyLibrary.App_Code.Entities) as MyLibrary.Entities

namespace MyLibrary.Entities
{
    public class Foo
    {
        ......
    }
}

Option 2: Using Directive

using Entities = MyLibrary.App_Code.Entities;

namespace MyApplication
{
    class Main
    {
        var foo = new Entities.Foo();
    }
}

Let me know if you have any questions.

Upvotes: 2

Related Questions