C #: static member Inherited / interface?

Is there a way to require the class to have a specific abstract member? Something like that:

public interface IMaxLength { public static uint MaxLength { get; } } 

Or perhaps this:

 public abstract class ComplexString { public abstract static uint MaxLength { get; } } 

I would like some type (either through inheritance or an interface?) To have a static member. It can be done?

+2
source share
4 answers

You can create a custom attribute that allows you to ensure that the requirement is met as a guarantee of execution time. This is not a complete code sample (you need to call VerifyStaticInterfaces when you launch your application, and you need to fill in the marked TODO), but it shows the basic information.

I assume that you are asking about this to guarantee successful reflection-based calls to named methods.

 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false, AllowMultiple = true)] internal sealed class StaticInterfaceAttribute : Attribute { private readonly Type interfaceType; // This is a positional argument public StaticInterfaceAttribute(Type interfaceType) { this.interfaceType = interfaceType; } public Type InterfaceType { get { return this.interfaceType; } } public static void VerifyStaticInterfaces() { Assembly assembly = typeof(StaticInterfaceAttribute).Assembly; Type[] types = assembly.GetTypes(); foreach (Type t in types) { foreach (StaticInterfaceAttribute staticInterface in t.GetCustomAttributes(typeof(StaticInterfaceAttribute), false)) { VerifyImplementation(t, staticInterface); } } } private static void VerifyInterface(Type type, Type interfaceType) { // TODO: throw TypeLoadException? if `type` does not implement the members of `interfaceType` as public static members. } } internal interface IMaxLength { uint MaxLength { get; } } [StaticInterface(typeof(IMaxLength))] internal class ComplexString { public static uint MaxLength { get { return 0; } } } 
+5
source

It's impossible. Since calls to abstract and virtual methods are stored in the object, although its table is a pointer to a virtual function, you cannot ensure that any interface requirements for its members are met without an instance. Calling a static member is not bound to an object, so there is no virtual pointer table.

This is not a limitation; it is just how it is. There is no reason why this would be necessary or useful. If you want to use the interface, you have to do this, although instance members.

+4
source

Impossible. Maybe you can try something like this:

 public class Base { public struct MyStruct { public static int x = 100; public static int XX() { return 200; } } } public class Derived : Base { public void test() { int x = Derived.MyStruct.x; int XX = Derived.MyStruct.XX(); } } 

Literature:

0
source

Suppose the Base class includes the StaticMethod static method and the InstanceMethod instance method, both of which return Int32. Class Derivative shadows of both of these methods with similar methods that return String.

If one instance of the derivative is in the base and calls the InstanceMethod instance, the call will use Base.InstanceMethod, whose return type is Int32. If you take an instance of type type T, where T inherits the base and calls the InstanceMethod instance, it will also call Base.InstanceMethod - again Int32. But what should be the meaning and return type of T.StaticMethod? If you need Base.StaticMethod, you should specify this. What else can T.StaticMethod mean?

0
source

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


All Articles