在 C# 中别名多个 类
Aliasing multiple classes in C#
我想(正在尝试)使我的代码更具可读性。我一直在使用以下 class 别名。
using Histogram = EmpiricScore<int>;
using FeatureHistogram = Dictionary<string, EmpiricScore<int>>;
但我想是这样的(注意:我试图在这里用 Histogram
来描述 FeatureHistogram
,而不是 EmpiricScore<int>>
):
using Histogram = EmpiricScore<int>;
using FeatureHistogram = Dictionary<string, Histogram>;
似乎更具可读性(依赖关系可以更深,如果我创建一个分层特征直方图会怎样),并且更容易重构(如果我碰巧认为直方图这个名字不合适)。但是编译器不会这样做。为什么 ?有什么办法可以避免这种情况?
创建新的 classes 似乎有点矫枉过正...
Creating new classes seems a little bit overkill...
我不觉得这太过分了,因为如果你设计一个 class 来包装 Dictionary<string, Histogram>
(你的 class 应该实现 IDictionary<string, Histogram>
并且有一个私有的 Dictionary<string, Histogram>
属性 支持数据)你在强制执行 可重用性 ,这是面向对象编程的最佳卖点之一。
例如,您的实现如下所示:
public class FeatureHistorgram : IDictionary<string, Historam>
{
private readonly Dictionary<string, Histogram> _data = new Dictionary<string, Histogram>();
public void Add(string key, Histogram value)
{
_data.Add(key, value);
}
// ... and the rest of IDictionary<TKey, TValue> interface members...
}
但是编译器不会这样做。为什么?
编译器不会根据 C# 规范 9.4.1 执行此操作:
A using-alias-directive
引入了一个标识符,用作直接封闭的编译单元或命名空间主体中命名空间或类型的别名。
using-alias-directive:
using identifier = namespace-or-type-name ;
The order in which using-alias-directives
are written has no significance, and resolution of the namespace-or-type-name
referenced by a using-alias-directive
is not affected by the using-alias-directive
itself or by other using-directives
in the immediately containing compilation unit or namespace body.
In other words, the namespace-or-type-name
of a using-alias-directive
is resolved as if the immediately containing compilation unit or namespace body had no using-directives.
namespace N1.N2 {}
namespace N3
{
using R2 = N1; // OK
using R3 = N1.N2; // OK
using R4 = R2.N2; // Error, R2 unknown
}
选项:
1. 按照评论中 M.kazem Akhgary
的建议,定义新的命名空间
using Histogram = System.Collections.Generic.List<int>;
namespace TEST
{
using FeatureHistogram = System.Collections.Generic.Dictionary<string, Histogram>;
public class Program
{
public static void Main()
{
var x = new Histogram();
Console.WriteLine(x.GetType());
var y = new FeatureHistogram();
Console.WriteLine(y.GetType());
}
}
}
- 创建 类 以获得更深层次的依赖性
我想(正在尝试)使我的代码更具可读性。我一直在使用以下 class 别名。
using Histogram = EmpiricScore<int>;
using FeatureHistogram = Dictionary<string, EmpiricScore<int>>;
但我想是这样的(注意:我试图在这里用 Histogram
来描述 FeatureHistogram
,而不是 EmpiricScore<int>>
):
using Histogram = EmpiricScore<int>;
using FeatureHistogram = Dictionary<string, Histogram>;
似乎更具可读性(依赖关系可以更深,如果我创建一个分层特征直方图会怎样),并且更容易重构(如果我碰巧认为直方图这个名字不合适)。但是编译器不会这样做。为什么 ?有什么办法可以避免这种情况?
创建新的 classes 似乎有点矫枉过正...
Creating new classes seems a little bit overkill...
我不觉得这太过分了,因为如果你设计一个 class 来包装 Dictionary<string, Histogram>
(你的 class 应该实现 IDictionary<string, Histogram>
并且有一个私有的 Dictionary<string, Histogram>
属性 支持数据)你在强制执行 可重用性 ,这是面向对象编程的最佳卖点之一。
例如,您的实现如下所示:
public class FeatureHistorgram : IDictionary<string, Historam>
{
private readonly Dictionary<string, Histogram> _data = new Dictionary<string, Histogram>();
public void Add(string key, Histogram value)
{
_data.Add(key, value);
}
// ... and the rest of IDictionary<TKey, TValue> interface members...
}
但是编译器不会这样做。为什么?
编译器不会根据 C# 规范 9.4.1 执行此操作:
A using-alias-directive
引入了一个标识符,用作直接封闭的编译单元或命名空间主体中命名空间或类型的别名。
using-alias-directive:
using identifier = namespace-or-type-name ;
The order in which
using-alias-directives
are written has no significance, and resolution of thenamespace-or-type-name
referenced by ausing-alias-directive
is not affected by theusing-alias-directive
itself or by otherusing-directives
in the immediately containing compilation unit or namespace body.In other words, the
namespace-or-type-name
of ausing-alias-directive
is resolved as if the immediately containing compilation unit or namespace body had no using-directives.
namespace N1.N2 {}
namespace N3
{
using R2 = N1; // OK
using R3 = N1.N2; // OK
using R4 = R2.N2; // Error, R2 unknown
}
选项:
1. 按照评论中 M.kazem Akhgary
的建议,定义新的命名空间
using Histogram = System.Collections.Generic.List<int>;
namespace TEST
{
using FeatureHistogram = System.Collections.Generic.Dictionary<string, Histogram>;
public class Program
{
public static void Main()
{
var x = new Histogram();
Console.WriteLine(x.GetType());
var y = new FeatureHistogram();
Console.WriteLine(y.GetType());
}
}
}
- 创建 类 以获得更深层次的依赖性