public class A { public static Type Method() { return /*Получить тип класса*/; } } public class B : A { } public void Test() { Console.WriteLine(A.Method().ToString()); Console.WriteLine(B.Method().ToString()); } |
1 answer
For a static method, this can not be done. Let's look at the IL code of the Test method:
IL_0000: nop IL_0001: call UserQuery+A.Method IL_0006: callvirt System.Object.ToString IL_000B: call System.Console.WriteLine IL_0010: nop IL_0011: call UserQuery+A.Method IL_0016: callvirt System.Object.ToString IL_001B: call System.Console.WriteLine IL_0020: nop IL_0021: ret It can be seen that in both cases the A.Method method is A.Method : since the method is static, its instructions will be placed in memory in a single instance during type A initialization.
- Now you saddened me = (. Well, thanks anyway. - Dmitry Chistik
|
A- Grundy