我如何别名C#类的名字吗? [英] How do I alias a class name in C#?

查看:144
本文介绍了我如何别名C#类的名字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

public class LongClassNameOrOneThatContainsVersionsOrDomainSpecificName
{
   ...
}

public class MyName = LongClassNameOrOneThatContainsVersionOrDomainSpecificName;

但它不会编译。


注意仅供便于学习这个例子。不要试图通过建议改变整个系统的设计来解决这个特别的问题。在presence,或缺乏,这个例子不改变原来的问题。

Note This example is provided for convience 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.

一些现有的$ C $,c取决于静态类的presence:

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

但我仍然面临着的$ C $,c取决于一个叫做到ColorScheme静态类的presence的事实。我首先想到的是创建色彩方案类,我会无论从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 descend from either Outlook2003 or Outlook2007:

public static class ColorScheme : Outlook2007ColorScheme
{
}

但你不能从一个静态类下降。

but you cannot descend from a static class.

我的下一个念头,就是创建静态到ColorScheme类,但要Outlook2003ColorScheme和Outlook2007ColorScheme类非静态的。然后在静态到ColorScheme类的静态变量可以指向真实色彩方案:

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

但这需要我转换一个只读静态色彩组成entirly到重写属性的类,然后我到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;

但是,这并不编译。

But that doesn't compile.

我怎样才能别名静态类到另一个名字吗?

How can i alias a static class into another name?


更新:可有人请添加答案的你不能在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.

我只是想关闭这个问题了。

i just want to close this question out.

推荐答案

您不能别名在C#中的类名。

You cannot alias a class name in C#.

有事情可以做,不走样在C#中的类名。

There are things you can do that are not aliasing 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);
}

Col​​orScheme.cs

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

和一切正常。现在我想创建一个的新的的类,的别名到ColorScheme 给它(这样的没有code需要修改

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

Col​​orScheme.cs

using ColorScheme = Outlook2007ColorScheme;

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

喔,我很抱歉。这code不能编译:

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

我的问题是如何的别名的在C#中的一类。它不能这样做。有件事情我可以做的是的的在C#别名类名称:

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#:


  • 改变大家谁取决于到ColorScheme 使用 到ColorScheme 而不是(code修改的解决方法,因为我无法别名)

  • 改变大家谁取决于到ColorScheme 使用工厂模式他们一个多态类或接口(code修改的解决方法,因为我无法别名)

  • 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)

但这些替代方法涉及破坏现有的code:不是一个选项。

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

如果人们依赖于到ColorScheme 类的presence,我必须真正复制/粘贴到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天全站免登陆