Exploring Information "Method" using Reflection?

My intention is to investigate the "Methods" Type using reflection to check the following:

  • Methods must be instance methods and public.

  • Takes a "params" parameter and is void in nature.

  • The method does not make a recursive call.

I started like:

static void ProcessMethodInfo(Type t)
    {
        MethodInfo[] info = t.GetMethods();

        foreach (MethodInfo mi in info)
        {

          // How to check the conditions here ?  

        }
    }

      

But I don't know how to move on. Do you need help.

+2


source to share


4 answers


It's good if by 3 you understand that the method being tested must be non-recursive; then it's a pain - you will need to parse the IL. But for the rest;



    Type type = ...
    var qry = from method in type.GetMethods(
                  BindingFlags.Instance | BindingFlags.Public)
              where method.ReturnType == typeof(void)
              let parameters = method.GetParameters()
              where parameters.Length == 1
              && parameters[0].ParameterType.IsArray
              && Attribute.IsDefined(parameters[0], typeof(ParamArrayAttribute))
              select method;
    foreach (var method in qry)
    {
        Console.WriteLine(method.Name);
    }

      

+3


source




ALL: use google;)

+1


source


I don't think you will be able to detect element 3 using reflection.

0


source


Check the following members of the MethodInfo class:

  • IsPublic
  • IsStatic
  • ReturnType
  • GetParameters () method

To test if a method is recursive, I think you need more than just simple reflection.

0


source







All Articles