如何在 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章