如何在 C# 中为类名设置别名,而不必向使用该类的每个文件添加一行代码?

问题:

I want to create an alias for a class name.我想为类名创建别名。 The following syntax would be perfect:以下语法将是完美的:

public class LongClassNameOrOneThatContainsVersionsOrDomainSpecificName
{
   ...
}

public class MyName = LongClassNameOrOneThatContainsVersionOrDomainSpecificName;

but it won't compile.但它不会编译。


Example例子

Note This example is provided for convenience only.注意此示例仅为方便起见而提供。 Don't try to solve this particular problem by suggesting changing the design of the entire system.不要试图通过建议更改整个系统的设计来解决这个特定问题。 The presence, or lack, of this example doesn't change the original question.这个例子的存在或缺失不会改变最初的问题。

Some existing code depends on the presence of a static class:一些现有代码取决于静态类的存在:

public static class ColorScheme
{
   ...
}

This color scheme is the Outlook 2003 color scheme.此配色方案是 Outlook 2003 配色方案。 i want to introduce an Outlook 2007 color scheme, while retaining the Outlook 2003 color scheme:我想介绍一个 Outlook 2007 配色方案,同时保留 Outlook 2003 配色方案:

public static class Outlook2003ColorScheme
{
   ...
}

public static class Outlook2007ColorScheme
{
   ...
}

But i'm still faced with the fact that the code depends on the presence of a static class called ColorScheme .但我仍然面临这样一个事实,即代码取决于一个名为ColorScheme的静态类的存在。 My first thought was to create a ColorScheme class that I will inherit from either Outlook2003 or Outlook2007 :我的第一个想法是创建一个ColorScheme类,我将从Outlook2003Outlook2007继承:

public static class ColorScheme : Outlook2007ColorScheme
{
}

but you cannot inherit from a static class.但是不能从静态类继承。

My next thought was to create the static ColorScheme class, but make Outlook2003ColorScheme and Outlook2007ColorScheme classes non-static.我的下一个想法是创建静态ColorScheme类,但使Outlook2003ColorSchemeOutlook2007ColorScheme类非静态。 Then a static variable in the static ColorScheme class can point to either "true" color scheme:然后静态ColorScheme类中的静态变量可以指向任一“真”配色方案:

public static class ColorScheme
{
    private static CustomColorScheme = new Outlook2007ColorScheme();
    ...
}

private class CustomColorScheme 
{ 
   ...
}

private class Outlook2008ColorScheme : CustomColorScheme 
{
    ...
}

private class Outlook2003ColorScheme : CustomColorScheme 
{
   ...
}

but that would require me to convert a class composed entirly of readonly static Colors into overridable properties, and then my ColorScheme class would need to have the 30 different property getters thunk down into the contained object.但这需要我将一个完全由只读静态颜色组成的类转换为可覆盖的属性,然后我的ColorScheme类需要将 30 个不同的属性 getter 转换为包含的对象。

That's just too much typing.打字太多了。

So my next thought was to alias the class:所以我的下一个想法是给这个类取别名:

public static ColorScheme = Outlook2007ColorScheme;

But that doesn't compile.但这不能编译。

How can I alias a static class into another name?如何将静态类别名为另一个名称?


Update: Can someone please add the answer "You cannot do this in C#" , so I can mark that as the accepted answer.更新:有人可以添加答案"You cannot do this in C#" ,所以我可以将其标记为已接受的答案。 Anyone else wanting the answer to the same question will find this question, the accepted answer, and a number of workarounds that might, or might not, be useful.其他任何想要回答同一问题的人都会发现这个问题、已接受的答案以及一些可能有用也可能没有用的解决方法。

I just want to close this question out.我只想结束这个问题。


解决方案:

参考: https://stackoom.com/en/question/11XS
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章