在一个命名空间中的不同文件之间共享类型别名

时间:2020-05-29 17:30:58

标签: c#

我已经定义了类型别名,并且可以在该文件中声明的类中使用它。

open('\gg.csv')

但是另一个文件中的另一个类,在相同的名称空间中,则不起作用。

using TemplateStructure = System.Collections.Generic.List<System.Collections.Generic.List<DataManagement.TemplateImageLocation>>;

namespace Gui.ViewModels
{
    public class AddRemoveCellsViewModel : TemplateWorkflowStepViewModel
    {
        public TemplateViewModel TemplateVM
        {
            get;
        }

        public AddRemoveCellsViewModel(TemplateStructure locations) : base(locations)
        {
            base.DisplayName = "Add and Remove Rows/Columns";
            TemplateVM = new TemplateViewModel();
        }
    }
}

而且,当然,使用必须放在名称空间声明之外。

是否可以在多个文件中使用此类型别名?

1 个答案:

答案 0 :(得分:2)

你必须写

using TemplateStructure = System.Collections.Generic.List<System.Collections.Generic.List<DataManagement.TemplateImageLocation>>;

在每个要使用此别名的文件中,因为

using_alias_directive 引入了一个标识符,该标识符用作立即封闭的编译单元或命名空间主体中的命名空间或类型的别名。

(来自C# Language Specification - Using alias directives

使用别名不适用于编译单元,它们是:

C#程序由一个或多个编译单元组成,每个都包含在一个单独的源文件中。

(来自C# Language Specification - Compilation units

相关问题