通过从父级继承的内部 class 的 C#.NET 反射,外部派生 class 是否可见?

Is outer derived class visible through C#.NET reflection of inner class inherited from parent?

在下面的代码示例中,我想从 class A 继承 class B 并且如果我只有 [=23] 类型的对象,则能够获得 class B 的名称=].

所以我的主题问题可以指定为:Is class B visible through C#.NET reflection of B.AC ?

根据接受的答案 - 答案是 "no",我必须在 B 中声明 new class AC 并从 A.AC.

派生它
using System;
namespace Question{
    class Program{
        class A{
           public class AC{}
        }
        class B: A{
           //new public class AC:A.AC{} // work around to get B+AC
        }
        class D: A{
           new public class AC:A.AC{} // work around to get D+AC
           public class DAC:A.AC{} // just another example
        }
        static void Main(string[] args)
        {
            var b_ac = new B.AC{}.GetType();
            var B_AC = typeof(B.AC);
            var D_AC = typeof(D.AC);
            var DAC = typeof(D.DAC);
            Func<Type,string> enclosure = (x)=>{
                Console.WriteLine(x.DeclaringType.Name);  // gives A - wrong
                Console.WriteLine(x.BaseType.Name);  // gives Object - wrong
                Console.WriteLine(x.FullName);  // gives A+AC - wrong, I want B+AC
                Console.WriteLine(x.ReflectedType.FullName);  // gives A - wrong
                return "B"; // I want to get name of class B through reflection
            };

           Console.WriteLine(enclosure(b_ac)); // gives A+AC - wrong, see details above
           Console.WriteLine(enclosure(B_AC)); // gives same wrong
           Console.WriteLine(enclosure(D_AC)); // gives D+AC - good
           Console.WriteLine(enclosure(DAC)); // gives D+DAC - good
           Console.ReadKey();
         }
    }
}

是的,您必须使用 new 重新定义 AC 才能执行此操作。如果您使用 ILSpy 之类的工具查看反射代码,您会发现它实际上是将行 var b_ac = new B.AC{}.GetType(); 构造为 newobj instance void Question.Program/A/AC::.ctor()。它正在调用 A 中定义的构造函数,就好像您已经完成了 new A.AC{}.GetType().