Can a class be an alias in C #?

Can you provide an alias for a class in the namespace? And if not, why not?

For example, if I had several libraries of things that were obtained from the contained, but called base class, but wanted the alias to be “BaseClass”, while preserving its actual class name (for example, “HtmlControl”).

Then consumers can always come and go from HtmlControls.BaseClass without figuring out which class it really comes from.

+3
source share
4 answers

There is no perfect way to do this in C # /. NET. What you can do is have an open BaseClass that inherits from the inner class. You can change this inheritance internally without breaking your consumers while the interface to the class remains intact.

public class PublicBaseClass : SomeInternalClass {

}

Consumers inherit from PublicBaseClass, and while you are careful, you can change that SomeInternalClassas you wish.

+1
source
using SomeClass = Large.Namespace.Other.FunkyClass;

class Foo : SomeClass
{
}
+4
source

, HtmlControl - :

public class BaseClass : HtmlControl {}
+1

, , using:

using BaseClass = HtmlControls.BaseClass;

, , . , , , , .

As for the conclusion BaseClass, not knowing what you are actually coming out of, this is impossible. The compiler must at some level know what and where the parent class is located, that is, it must be statically defined somewhere in the code.

0
source

Source: https://habr.com/ru/post/1780079/


All Articles