chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
 
namespace UtilLib
{
    public class MethodInfoHelper
    {
        public static List<MethodInfo> GetPublicMethodNames(Type type)
        {
            if (type == null)
                return new List<MethodInfo>();
 
            var methods = type.GetMethods(BindingFlags.Public |
                                    BindingFlags.Instance |
                                    BindingFlags.Static);
 
            // 排除Object类的方法和事件相关方法
            var objectMethods = typeof(Object).GetMethods(BindingFlags.Public |
                                                        BindingFlags.Instance |
                                                        BindingFlags.Static)
                                            .Select(m => m.Name)
                                            .ToHashSet();
 
            // 获取所有事件
            var events = type.GetEvents(BindingFlags.Public |
                                      BindingFlags.Instance |
                                      BindingFlags.Static);
 
            // 获取所有事件相关的方法名
            var eventMethods = events.SelectMany(e => new[]
            {
            $"add_{e.Name}",
            $"remove_{e.Name}",
            $"raise_{e.Name}"
        }).ToHashSet();
 
            // 过滤出带有ExportMethodAttribute注解的方法
            return methods.Where(m => !objectMethods.Contains(m.Name) &&
                                    !eventMethods.Contains(m.Name))
                         .ToList();
        }
 
        // 重载方法,直接传入对象实例
        public static List<MethodInfo> GetPublicMethodNames(object obj)
        {
            if (obj == null)
                throw new ArgumentNullException(nameof(obj));
 
            return GetPublicMethodNames(obj.GetType());
        }
    }
}