C# 使用反射从基础类型获取方法名称

发布于 2024-12-25 04:00:20 字数 460 浏览 1 评论 0原文

我想列出底层类型的所有方法名称。

我尝试过

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()
   {}
}

在集合 Method1 和 Method 2 中

I'd like to list all method names from the underlyingtype.

I tried

var methods = this.GetType().UnderlyingSystemType.GetMethods();

but doesn't work.

EDIT

Added example

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

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

   public void Method2()
   {}
}

I need in the collection Method1 and Method 2.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

智商已欠费 2025-01-01 04:00:20

您提供的代码有效。

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);
}

The code you provided works.

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

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

returns

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

EDIT:

Is that what you want?

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);
}
屌丝范 2025-01-01 04:00:20

尝试类似的东西

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

Try something like

MethodInfo[] methodInfos =
typeof(MyClass).GetMethods(BindingFlags.Public |
                                                      BindingFlags.Static);
岛歌少女 2025-01-01 04:00:20
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());
 }
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());
 }
白衬杉格子梦 2025-01-01 04:00:20

问题在于您在 BaseClass 的构造函数中调用的 GetType 的重写。

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

如果您创建 BaseClass 类型的实例,您将只会看到 4 个方法 - 来自 Object 类型的 4 个方法。

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

The problem lies in the override of GetType which you are calling in the constructor of the BaseClass.

If you create an instance of the Class1 type, and look at the methods you have, you will see all 6 methods.

If you create an instance of the BaseClass type, you will only see 4 methods - the 4 methods on from the Object type.

By creating an instance of the subclass, you are implicitly calling the constructor in the BaseClass. When it uses GetType(), it uses the overridden virtual method of the Class1 type, which returns the expected response.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文