简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM