c#使用反射从底层类型获取方法名称

c#使用反射从底层类型获取方法名称,c#,reflection,C#,Reflection,我想列出UnderlineType中的所有方法名称 我试过了 var methods = this.GetType().UnderlyingSystemType.GetMethods(); 但是不起作用 编辑 添加示例 public class BaseClass { public BaseClass() { var methods = this.GetType().UnderlyingSystemType.GetMethods(); } } public

我想列出UnderlineType中的所有方法名称

我试过了

var methods = this.GetType().UnderlyingSystemType.GetMethods();
但是不起作用

编辑

添加示例

public class BaseClass
{
   public BaseClass()
   {
         var methods = this.GetType().UnderlyingSystemType.GetMethods();
   }
}

public class Class1:BaseClass
{
   public void Method1()
   {}

   public void Method2()
   {}
}
我需要输入收集方法1和方法2。

尝试类似的方法

MethodInfo[] methodInfos =
typeof(MyClass).GetMethods(BindingFlags.Public |
                                                      BindingFlags.Static);

您提供的代码有效

System.Exception test = new Exception();
var methods = test.GetType().UnderlyingSystemType.GetMethods();

foreach (var t in methods)
{
    Console.WriteLine(t.Name);
}
返回

get_Message
get_Data
GetBaseException
get_InnerException
get_TargetSite
get_StackTrace
get_HelpLink
set_HelpLink
get_Source
set_Source
ToString
GetObjectData
GetType
Equals
GetHashCode
GetType
编辑:

这就是你想要的吗

Class1 class1 = new Class1();
var methodsClass1 = class1.GetType().GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

BaseClass baseClass = new BaseClass();
var methodsBaseClass = baseClass.GetType().GetMethods(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

foreach (var t in methodsClass1.Where(z => methodsBaseClass.FirstOrDefault(y => y.Name == z.Name) == null))
{
    Console.WriteLine(t.Name);
}

问题在于在基类的构造函数中调用的GetType的重写

如果您创建Class1类型的实例,并查看您拥有的方法,您将看到所有6个方法

如果创建基类类型的实例,则只会看到4个方法—对象类型上的4个方法


通过创建子类的实例,可以隐式调用基类中的构造函数。当它使用GetType()时,它使用Class1类型的重写虚拟方法,该方法返回预期的响应。

基础类型是什么?还有什么回报呢?需要更多信息请了解基本类型是什么?发生了什么?“不起作用”从来都不是一个好的描述。请阅读并更新您的问题,了解更多详细信息。您不需要BaseType而不是UnderlineingSystemType吗?对我来说没有多大意义,但如果您实际需要的是Method1和Method2,请参阅我的更新答案…我需要在baseclass构造函数中进行此调用
here is an example on how to use reflection to get the Method names
replace MyObject with your Object / Class

using System.Reflection;
MyObject myObject;//The name of the Object
foreach(MethodInfo method in myObject.GetType().GetMethods())
 {
    Console.WriteLine(method.ToString());
 }