如何在C#中为类名加上别名,而不必在使用该类的每个文件中添加一行代码? [英] How do I alias a class name in C#, without having to add a line of code to every file that uses the class?

查看:71
本文介绍了如何在C#中为类名加上别名,而不必在使用该类的每个文件中添加一行代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为类名创建一个别名。以下语法将是完美的:

I want to create an alias for a class name. The following syntax would be perfect:

public class LongClassNameOrOneThatContainsVersionsOrDomainSpecificName
{
   ...
}

public class MyName = LongClassNameOrOneThatContainsVersionOrDomainSpecificName;

,但不会编译。

注意本示例仅出于方便而提供。不要通过建议更改整个系统的设计来尝试解决此特定问题。此示例的存在与否不会改变原始问题。

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
{
   ...
}

此配色方案是Outlook 2003配色方案。我想引入一个Outlook 2007配色方案,同时保留Outlook 2003配色方案:

This color scheme is the Outlook 2003 color scheme. i want to introduce an Outlook 2007 color scheme, while retaining the Outlook 2003 color scheme:

public static class Outlook2003ColorScheme
{
   ...
}

public static class Outlook2007ColorScheme
{
   ...
}

但是我仍然面对这样一个事实,即代码依赖于名为 ColorScheme 的静态类的存在。我的第一个想法是创建一个 ColorScheme 类,该类将继承自 Outlook2003 Outlook2007

But i'm still faced with the fact that the code depends on the presence of a static class called ColorScheme. My first thought was to create a ColorScheme class that I will inherit from either Outlook2003 or Outlook2007:

public static class ColorScheme : Outlook2007ColorScheme
{
}

,但是您不能从静态类继承。

but you cannot inherit from a static class.

我的下一个想法是创建静态 ColorScheme 类,但将 Outlook2003ColorScheme Outlook2007ColorScheme 类设为非静态。然后,静态 ColorScheme 类中的静态变量可以指向 true或 true。配色方案:

My next thought was to create the static ColorScheme class, but make Outlook2003ColorScheme and Outlook2007ColorScheme classes non-static. Then a static variable in the static ColorScheme class can point to either "true" color scheme:

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

private class CustomColorScheme 
{ 
   ...
}

private class Outlook2008ColorScheme : CustomColorScheme 
{
    ...
}

private class Outlook2003ColorScheme : CustomColorScheme 
{
   ...
}

但这需要我将完全由只读静态Colors组成的类转换为可覆盖的属性,然后转换为 ColorScheme 类需要将30个不同的属性获取器重新放入所包含的对象中。

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.

键入的内容太多。

所以我的下一个想法是对该类进行别名:

So my next thought was to alias the class:

public static ColorScheme = Outlook2007ColorScheme;

但是不能编译。

如何为静态类添加别名

更新::有人可以添加答案您不能在C#中执行此操作吗? ,因此我可以将其标记为已接受的答案。任何想要同一问题的答案的人都会找到这个问题,被接受的答案以及一些可能有用或可能无效的解决方法。

Update: Can someone please add the answer "You cannot do this in C#", so I can mark that as the accepted answer. 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.

我只想关闭此问题

推荐答案

您不能在C#中为类名加上别名。

You cannot alias a class name in C#.

但是要回答最初的问题:您不能在C#中为类名加上别名。

But to answer the original question: you cannot alias a class name in C#.

更新:人们感到困惑,为什么使用不起作用。示例:

Update: People are confused why using doesn't work. Example:

Form1.cs

private void button1_Click(object sender, EventArgs e)
{
   this.BackColor = ColorScheme.ApplyColorScheme(this.BackColor);
}

ColorScheme.cs

class ColorScheme
{
    public static Color ApplyColorScheme(Color c) { ... }
}

一切正常。现在,我想创建一个 new 类,并为其创建 alias ColorScheme (这样,无需任何代码进行修改):

And everything works. Now i want to create a new class, and alias ColorScheme to it (so that no code needs to be modified):

ColorScheme.cs

using ColorScheme = Outlook2007ColorScheme;

class Outlook2007ColorScheme
{
    public static Color ApplyColorScheme(Color c) { ... }
}

哦,很抱歉。该代码无法编译:

Ohh, i'm sorry. This code doesn't compile:

我的问题是如何在C#中别名 一个类。无法完成。我可以做的事情不是在C#中为类名 not

My question was how to alias a class in C#. It cannot be done. There are things i can do that are not aliasing a class name in C#:


  • 更改所有依赖<$ c $的人c> ColorScheme 改为使用 ColorScheme (由于我无法别名,所以代码更改了解决方法)

  • 更改所有依赖 ColorScheme 的人,以工厂模式将它们用作多态类或接口(代码更改解决方法,因为我无法使用别名)

  • change everyone who depends on ColorScheme to using ColorScheme instead (code change workaround because i cannot alias)
  • change everyone who depends on ColorScheme to use a factory pattern them a polymorphic class or interface (code change workaround because i cannot alias)

但是这些解决方法涉及破坏现有代码:不是一种选择。

But these workarounds involve breaking existing code: not an option.

如果人们依赖 ColorScheme 类,我必须实际复制/粘贴 ColorScheme 类。

If people depend on the presence of a ColorScheme class, i have to actually copy/paste a ColorScheme class.

换句话说:我不能在C#中为类名加上别名。

In other words: i cannot alias a class name in C#.

这与其他面向对象的语言形成了别名:

This contrasts with other object oriented languages, where i could define the alias:

ColorScheme = Outlook2007ColorScheme

我会做完。

这篇关于如何在C#中为类名加上别名,而不必在使用该类的每个文件中添加一行代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆