简体   繁体   中英

How to access a type's implemented static abstract interface members by having only a type without using reflection?

How to access a type's implemented static abstract interface members by having only a type and using the interface type itself? Any way to do that? I'm not talking about accessing static members via reflection.

example:

public interface ISomeInterface
{
   static int SomeProperty {get; set;}
}

public class SomeClass : ISomeInterface
{
   static abstract int SomeProperty {get; set;} = 2;
}

var implementingType = typeof(SomeClass);

ISomeInterface interface = /* How? */ 

Is there any way of getting the value without using the common way by using reflection to acces a static member like this

void Method {
   var value = typeof(SomeClass).GetProperty("SomeProperty").GetValue(null);
   value = typeof(ISomeInterface).GetProperty("SomeProperty").GetValue(null);
}

Casting is a concept which only works with instances so it cannot be used for Type definitions.

After thinking again over the question and what I wanted to do, the answer is no.

There is no way of using the interface type to access the overridden value of a static abstract interface member in an implementing class other than reflection and accessing the static prop by using

GetProperty(name).GetValue(null)

Edit: SO is not the place for suggestions, but I thought about something like:

ISomeInterface a = (ISomeInterface)typeof(SomeImplementingClass);
var value = a.SomeInterface.SomeProperty;

or

var value = typeof(SomeImplementingClass).As<ISomeInterface>().SomeProperty;
var value = ISomeInterface.FromType<SomeImplementingClass>().SomeProperty;

...where '.As()' throws an exception if the interface isnt implemented. Both of these examples aren't supported.

First, abstract keyword should be applied to interface method, not class. If interface method is not abstract your question is pointless anyway.

Static abstract interface methods are irrevelant here because you cant call any static method without specifying type name, in other words you cant call static methods on instances.

Fallowing is my approach to your question, which is as almost as fast as direct call (it cant be inlined though.) Using static readonly managed function pointers to hold constructed method addresses we can avoid delegate object creation and method address will be treated as constant value by jit but no need to complicate things here..

class Program
{

    public interface ISomeInterface
    {
        abstract static int SomeProperty { get; set; }
    }

    public class SomeClass : ISomeInterface
    {
        static int ISomeInterface.SomeProperty { get; set; } = 2;
    }

    public class SomeClass2 : ISomeInterface
    {
        static int ISomeInterface.SomeProperty { get; set; } = 3;
    }

    public static int GetProp<T>() where T : ISomeInterface
    {
        return T.SomeProperty;
    }

    public static void Main(string[] args)
    {
        var obj = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(SomeClass)) as ISomeInterface;
        //obj.SomeProperty <= no hopes here..

        var methodDefinitation = typeof(Program).GetMethod(nameof(GetProp), System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);

        var @delegate = methodDefinitation.MakeGenericMethod(typeof(SomeClass)).CreateDelegate(typeof(Func<int>)) as Func<int>;
        var @delegate2 = methodDefinitation.MakeGenericMethod(typeof(SomeClass2)).CreateDelegate(typeof(Func<int>)) as Func<int>;
        var result1 = @delegate(); //2
        var result2 = @delegate2(); //3
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM