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.
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.
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.
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.
You can also create an alias for a namespace or a type with a using alias directive.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With