[英]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?如何通过只有一个类型并使用接口类型本身来访问一个类型实现的 static抽象接口成员? Any way to do that?
有什么办法吗? I'm not talking about accessing static members via reflection.
我不是在谈论通过反射访问 static 成员。
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有没有什么方法可以在不使用常用方法的情况下通过使用反射来访问这样的 static 成员来获取值
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除了反射和通过使用访问 static 属性之外,没有其他方法可以使用接口类型来访问实现 class 中的 static 抽象接口成员的覆盖值
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. ...如果未实现接口,则 '.As()' 将引发异常。 Both of these examples aren't supported.
不支持这两个示例。
First, abstract keyword should be applied to interface method, not class.首先,抽象关键字应该应用于接口方法,而不是 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. Static 抽象接口方法在这里是无关紧要的,因为你不能在没有指定类型名称的情况下调用任何 static 方法,换句话说你不能在实例上调用 static 方法。
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.. Fallowing 是我对您的问题的解决方法,它几乎与直接调用一样快(尽管它不能被内联。)使用 static 只读管理 function 指针来保存构造的方法地址,我们可以避免委托 ZA8CFDE 方法6331BD59EB26AC96 创建和将被视为常量 ZA8CFDE 方法 6331BD59EB2696B jit 的价值,但不需要在这里使事情复杂化..
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
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.