Get only Methods with specific signature out of Type.GetMethods()

后端 未结 5 1414
梦毁少年i
梦毁少年i 2020-12-03 18:23

I want to list all methods of a type with a specific method signature.

For example, if a type has a few public methods:

public void meth1 (int i);
pu         


        
相关标签:
5条回答
  • 2020-12-03 18:44

    This is the extension method to use improved from EvgK's answer.

     private static IEnumerable<MethodInfo> GetMethodsBySig(this Type type,
                                                                    Type returnType,
                                                                    Type customAttributeType,
                                                                    bool matchParameterInheritence,
                                                                    params Type[] parameterTypes)
            {
                return type.GetMethods().Where((m) =>
                {
                    if (m.ReturnType != returnType) return false;
    
                if ((customAttributeType != null) && (m.GetCustomAttributes(customAttributeType, true).Any() == false))
                    return false;
    
                var parameters = m.GetParameters();
    
                if ((parameterTypes == null || parameterTypes.Length == 0))
                    return parameters.Length == 0;
    
                if (parameters.Length != parameterTypes.Length)
                    return false;
    
                for (int i = 0; i < parameterTypes.Length; i++)
                {
                    if (((parameters[i].ParameterType == parameterTypes[i]) ||
                    (matchParameterInheritence && parameterTypes[i].IsAssignableFrom(parameters[i].ParameterType))) == false)
                        return false;
                }
    
                return true;
            });
        }
    

    Use it like

    var methods = SomeTypeToScan.GetMethodsBySig(
                    typeof(SomeReturnType),
                    typeof(SomeSpecialAttributeMarkerType),
                    true, 
                    typeof(SomeParameterType))
                    .ToList();
    
    0 讨论(0)
  • 2020-12-03 18:49

    You'll have to inspect all MethodInfos yourself. By calling MethodInfo.GetParameters() you'll get a collection of ParameterInfo objects, which in turn have a property ParameterType.

    The same for the return type: inspect the ReturnType property of MethodInfo.

    0 讨论(0)
  • 2020-12-03 18:57
    type.GetMethods().Where(p =>
                    p.GetParameters().Select(q => q.ParameterType).SequenceEqual(new Type[] { typeof(int), typeof(string) }) &&
                    p.ReturnType == typeof(void)
                );
    
    0 讨论(0)
  • 2020-12-03 19:01

    Given this class:

    public class Foo
    {
        public void M1(int i){}
        public void M2(int i, string s){}
        public void M3(int i, string s){}
        public int M4(int i, string s){ return 0; }
    }
    

    A bit of Reflection and LINQ can be used:

    Type t = typeof (Foo);
    var theMethods = t.GetMethods().Where(mi =>
                                {
                                    var p = mi.GetParameters();
                                    if (p.Length != 2)
                                        return false;
    
                                    if (p[0].ParameterType != typeof(int) 
                                         || p[1].ParameterType != typeof(string))
                                        return false;
    
                                    return mi.ReturnType == typeof (void);
                                });
    

    or the other syntax (which is actually nicer in this case)

    var theMethods = from mi in t.GetMethods()
                        let p = mi.GetParameters()
                        where p.Length == 2
                            && p[0].ParameterType == typeof (int)
                            && p[1].ParameterType == typeof (string)
                            && mi.ReturnType == typeof (void)
                        select mi;
    

    Test:

    foreach (var methodInfo in theMethods)
    {
        Console.WriteLine(methodInfo.Name);
    }
    

    Output:

    M2
    M3
    
    0 讨论(0)
  • 2020-12-03 19:06

    You can use something like this:

    public static class Extensions
    {
        public static IEnumerable<MethodInfo> GetMethodsBySig(this Type type, Type returnType, params Type[] parameterTypes)
        {
            return type.GetMethods().Where((m) =>
            {
                if (m.ReturnType != returnType) return false;
                var parameters = m.GetParameters();
                if ((parameterTypes == null || parameterTypes.Length == 0))
                    return parameters.Length == 0;
                if (parameters.Length != parameterTypes.Length)
                    return false;
                for (int i = 0; i < parameterTypes.Length; i++)
                {
                    if (parameters[i].ParameterType != parameterTypes[i])
                        return false;
                }
                return true;
            });
        }
    }
    

    And use it like this:

    var methods =  this.GetType().GetMethodsBySig(typeof(void), typeof(int), typeof(string));
    
    0 讨论(0)
提交回复
热议问题