Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I alias a class name in C#, without having to add a line of code to every file that uses the class?

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. i want to introduce an Outlook 2007 color scheme, while retaining the Outlook 2003 color scheme:

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

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

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.

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

like image 797
Ian Boyd Avatar asked Oct 28 '08 18:10

Ian Boyd


People also ask

How to alias a class in C#?

To alias a namespace or class name, use the using directive to define the alias as shown in the sample code below. Then you can use the alias anywhere you would normally use the class name or namespace. The scope of a using directive is limited to the file in which it appears.

What is alias in C sharp?

Introduction. Namespaces in C# serve two main purposes: to organize classes and functionality and to control the scope of classes and methods. Type aliasing is a little known feature for C# and it comes in handy when you would like to alias a specific method or class from a namespace for the sake of clarity.

Can we create alias of a namespace?

You can also create an alias for a namespace or a type with a using alias directive.

What is extern alias in C#?

Each extern alias declaration introduces an additional root-level namespace that parallels (but does not lie within) the global namespace. Thus types from each assembly can be referred to without ambiguity by using their fully qualified name, rooted in the appropriate namespace-alias.


1 Answers

You can’t. The next best thing you can do is have using declarations in the files that use the class.

For example, you could rewrite the dependent code using an import alias (as a quasi-typedef substitute):

using ColorScheme = The.Fully.Qualified.Namespace.Outlook2007ColorScheme; 

Unfortunately this needs to go into every scope/file that uses the name.

I therefore don't know if this is practical in your case.

like image 78
Konrad Rudolph Avatar answered Oct 14 '22 07:10

Konrad Rudolph