如何向另一个项目使用的命名空间添加较短的别名? [英] How do I add a shorter alias to a namespace used by another project?

查看:26
本文介绍了如何向另一个项目使用的命名空间添加较短的别名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好!

我在 Visual Studio 解决方案中有两个 C# 项目,它们具有以下命名空间结构(反映目录布局):

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

MyApplication 项目中,我想使用 MyLibrary 项目的 Entities 命名空间中的一些类,而无需指定其每次都完全限定名称:

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
    {
        ...
    }
}

如何在 MyLibrary 中将 MyLibrary.Entities 定义为 MyLibrary.App_Code.Entities 的别名(从而避免每次组件运行时都需要手动执行)使用)?

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

推荐答案

选项 1:将实体类 (MyLibrary.App_Code.Entities) 的命名空间命名为 MyLibrary.Entities

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

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

选项 2:使用指令

using Entities = MyLibrary.App_Code.Entities;

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

如果您有任何问题,请告诉我.

Let me know if you have any questions.

这篇关于如何向另一个项目使用的命名空间添加较短的别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆