Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

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);
public void meth2 (int i, string s);
public void meth3 (int i, string s);
public int meth4 (int i, string s);

I want to list all the methods which expect an int as first and a string as second parameter and returns void.

How can I do this?

like image 226
mabstrei Avatar asked Mar 01 '11 09:03

mabstrei


4 Answers

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));
like image 139
EvgK Avatar answered Nov 12 '22 23:11

EvgK


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();
like image 27
Dasith Wijes Avatar answered Sep 27 '22 15:09

Dasith Wijes


type.GetMethods().Where(p =>
                p.GetParameters().Select(q => q.ParameterType).SequenceEqual(new Type[] { typeof(int), typeof(string) }) &&
                p.ReturnType == typeof(void)
            );
like image 6
James Gaunt Avatar answered Nov 12 '22 23:11

James Gaunt


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.

like image 5
C.Evenhuis Avatar answered Nov 12 '22 23:11

C.Evenhuis