[英]Get Caller derived-class when calling a base-class static method
I was wondering if it's possible (even via reflection et similia) to get the caller derived-class inside of a called base-class static method. 我想知道是否有可能(甚至通过反射等)在调用的基类静态方法内部获取调用者的派生类。
For example, I've a base-class with a static method defined: 例如,我有一个定义了静态方法的基类:
public MyBaseClass {
public static void MyBaseClassStaticMethod() { /** ... **/ }
}
and a derived-from-it class: 和它的派生类:
public MyDerivedClass : MyBaseClass { }
then I call: 然后我打电话给:
MyDerivedClass.MyBaseClassStaticMethod()
Is it possibile, inside of method MyBaseClassStaticMethod
, to know which is the caller derived type ? 在方法MyBaseClassStaticMethod
,是否可以知道调用方的派生类型是什么?
(ie MyDerivedClass
) (即MyDerivedClass
)
I just need a string... 我只需要一个字符串...
Generics in following way can be used to solve your scenario 以下方式的泛型可用于解决您的情况
public class BaseClass<TDerived> where TDerived : BaseClass<TDerived>
{
public static void LogCallerType()
{
Console.WriteLine(typeof(TDerived).Name);
}
}
public class FooClass : BaseClass<FooClass> { }
public class BooClass : BaseClass<BooClass> { }
class Program
{
static void Main(string[] args)
{
FooClass.LogCallerType();
BooClass.LogCallerType();
}
}
This will in turn output the following 这将依次输出以下内容
1. FooClass
2. BooClass
No, this is not possible - by no means. 不,这是不可能的-绝不是。 static
methods are not polymorphal and as such this information simply doesn't exist. static
方法不是多态的,因此这些信息根本不存在。
Consider redesigning your code. 考虑重新设计代码。
Update: 更新:
Upon compilation, the compiler replaces MyDerivedClass
with the class the static method is actually declared on, in your case MyBaseClass
. 编译后,编译器将MyDerivedClass
替换为实际声明了静态方法的类,在您的情况下为MyBaseClass
。
So even in the IL you don't see MyDerivedClass
. 因此,即使在IL中,您也看不到MyDerivedClass
。 The information exists only in your source code. 该信息仅存在于您的源代码中。 It doesn't exist in your compiled assembly. 它在您的已编译程序集中不存在。
A static method is statically bound to a certain class and does not really participate in the inheritance-chain. 静态方法静态绑定到某个类,并且实际上不参与继承链。 Thus it does not exist in the derived class. 因此,它在派生类中不存在。 The static method therefor does not know that it was actually used in the derived class. 因此,静态方法不知道它实际在派生类中使用。
You can however - through a compiler-trick - access the static member from your derived class. 但是,您可以通过编译技巧来从派生类访问静态成员。 As of this post on MSDN-forum a static member-access from a derived class is translated to a call from the base-class containing the static member. 从MSDN论坛上的这篇文章开始,派生类的静态成员访问被转换为包含静态成员的基类的调用。 So MyDerivedClass.MyBaseClassStaticMethod
is translated to MyBaseClass.MyBaseClassStaticMethod
. 因此,将MyDerivedClass.MyBaseClassStaticMethod
转换为MyBaseClass.MyBaseClassStaticMethod
。 Thus MethodBase.GetCurrentMethod().DeclaringType
will allways return MyBaseClass
. 因此MethodBase.GetCurrentMethod().DeclaringType
MyBaseClass
将始终返回MyBaseClass
。
So in short: no, it´s not possible to get the derived type from a static member. 简而言之: 不,不可能从静态成员中获取派生类型。
First of all, the static method will not have access to the instance that is calling it. 首先,静态方法将无法访问正在调用它的实例。 A static method is different from a normal class method in that it does not have access the 'this' reference to a class instance. 静态方法与普通类方法的不同之处在于,它无法访问类实例的“ this”引用。
If you passed 'this' as a parameter to the static method, then you can try casting it as follows. 如果将“ this”作为参数传递给静态方法,则可以尝试按以下方式进行强制转换。 Assume you have a number of derived class which you want to test for. 假设您有许多要测试的派生类。
public static void MyBaseClassStaticMethod(MyBaseClass callingInstance)
{
MyDerivedClass myDerivedClass = callingInstance as MyDerivedClass;
MyDerivedClass2 myDerivedClass2 = callingInstance as MyDerivedClass2;
MyDerivedClass3 myDerivedClass3 = callingInstance as MyDefivedClass3;
...
// test for which derived class is calling
if (myDerivedClass != null)
...
else if (myDerivedClass2 != null)
...
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.