在C#中走样多个类 [英] Aliasing multiple classes in C#
问题描述
我想(我想)让我的代码更易读。我一直在使用下面的类混叠
使用直方图= EmpiricScore< INT取代;串,EmpiricScore<使用FeatureHistogram =字典<
INT>取代;
不过,我认为像(注:我试图描述 FeatureHistogram
在柱状图
在这里,而不是而言 EmpiricScore< INT>>
)
使用直方图= EmpiricScore< INT取代;使用FeatureHistogram =字典<
;串,直方图取代;
似乎更可读(的依赖性会要深刻得多,如果我创建分层特征直方图) ,更容易重新的因素(如果我碰巧来决定这个名字直方图是不幸的)。但是编译器不会去做。为什么呢?任何方式规避这一点?
创建新类似乎有点有点矫枉过正...
但是,编译器不会去做。 ?
A <$ C:为什么
编译器不会根据C#规范9.4.1做$ C>使用别名指示引入,可作为直接包含编译单元或命名空间体内的命名空间或类型别名的标识符。
使用别名指令:使用标识符=命名空间或类型名称
;
的顺序
使用-ALIAS-指令
写并不重要,而命名空间或类型名称
通过引用的分辨率使用别名-directive
不是受使用别名指示
本身或其他using指令
中直接包含编译单元或命名空间体。
在换句话说
using-时,
是因为如果直接包含编译单元或命名空间体没有using指令解决。命名空间或类型名称
别名指示
块引用>
命名空间N1.N2 {}
命名空间N3
使用R2 = N1 {
; //使用R3 = N1.N2确定
; //使用R4 = R2.N2确定
; //错误,R2未知
}
选项:
1.为M.kazem Akhgary
的评论所说,定义新的命名空间。
的演示使用直方图= System.Collections.Generic.List<
; INT取代;
命名空间TEST
{
使用FeatureHistogram = System.Collections.Generic.Dictionary<字符串,直方图取代;
公共类节目
{
公共静态无效的主要()
{
变种x =新的直方图();
Console.WriteLine(x.GetType());
变种Y =新FeatureHistogram();
Console.WriteLine(y.GetType());
}
}
}
- 更深的依赖关系创建类
I want to (am trying to) make my code more readable. I have been using the following class aliasing.
using Histogram = EmpiricScore<int>; using FeatureHistogram = Dictionary<string, EmpiricScore<int>>;
But I think something like (note: I'm attempting to describe
FeatureHistogram
in terms ofHistogram
here, rather thanEmpiricScore<int>>
):using Histogram = EmpiricScore<int>; using FeatureHistogram = Dictionary<string, Histogram>;
Seems more readable (the dependencies can go much deeper, what if I create a Hierarchical feature histogram), and easier to re-factor (if I happen to decide that the name Histogram is unfortunate). But the compiler won't do it. Why ? Any way to circumvent this ?
Creating new classes seems a little bit overkill...
解决方案But the compiler won't do it. Why ?
compiler won't do it according to C# specification 9.4.1:
A
using-alias-directive
introduces an identifier that serves as an alias for a namespace or type within the immediately enclosing compilation unit or namespace body.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 }
options: 1. as
M.kazem Akhgary
suggested in a comment, define new namespaceusing 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()); } } }
- create classes for deeper dependencies
这篇关于在C#中走样多个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!