Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Method Matching

Method Matching

Scheduled Pinned Locked Moved C#
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SHaroz
    wrote on last edited by
    #1

    Hello everyone, I am trying to find all methods that have a particular signature. In other words, I want all methods that match the return type and parameter types of a specified method. I have a solution, but I was wondering if a more elegant approach exists. Using delegates is one possibility, but delegates are not very “edit-and-continue” friendly. Here is my current solution:

    void findSimilarMethods()
    {
    MethodInfo example = this.GetType().GetMethod("ExampleMethod");
    ParameterInfo[] parameters = example.GetParameters();
    List matches = new List();

    MethodInfo\[\] methods = this.GetType().GetMethods();
    foreach (MethodInfo current in methods) {
        ParameterInfo\[\] currentParams = current.GetParameters();
        if ( current.ReturnType != example.ReturnType )
            continue;
        if ( currentParams.Length != parameters.Length )
            continue;
        if ( !ParameterEquality(parameters, currentParams) )
            continue;
        matches.Add(current);
    }
    

    }

    // a helper method to check parameter type equality
    bool ParameterEquality(ParameterInfo[] a, ParameterInfo[] b)
    {
    if (a.Length != b.Length)
    return false;
    for (int i = 0; i < a.Length; i++)
    if (a[i].GetType() != b[i].GetType())
    return false;
    return true;
    }

    Any suggestions would be greatly appreciated. -Steve

    S 1 Reply Last reply
    0
    • S SHaroz

      Hello everyone, I am trying to find all methods that have a particular signature. In other words, I want all methods that match the return type and parameter types of a specified method. I have a solution, but I was wondering if a more elegant approach exists. Using delegates is one possibility, but delegates are not very “edit-and-continue” friendly. Here is my current solution:

      void findSimilarMethods()
      {
      MethodInfo example = this.GetType().GetMethod("ExampleMethod");
      ParameterInfo[] parameters = example.GetParameters();
      List matches = new List();

      MethodInfo\[\] methods = this.GetType().GetMethods();
      foreach (MethodInfo current in methods) {
          ParameterInfo\[\] currentParams = current.GetParameters();
          if ( current.ReturnType != example.ReturnType )
              continue;
          if ( currentParams.Length != parameters.Length )
              continue;
          if ( !ParameterEquality(parameters, currentParams) )
              continue;
          matches.Add(current);
      }
      

      }

      // a helper method to check parameter type equality
      bool ParameterEquality(ParameterInfo[] a, ParameterInfo[] b)
      {
      if (a.Length != b.Length)
      return false;
      for (int i = 0; i < a.Length; i++)
      if (a[i].GetType() != b[i].GetType())
      return false;
      return true;
      }

      Any suggestions would be greatly appreciated. -Steve

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      Why not use one of the other overloads of GetMethod MSDN[^], like the one that takes the method name and a Type [] for the parameters? And you don't really need to match return type if you're matching parameter types, because you can't overload methods based on return type alone.

      Regards Senthil [MVP - Visual C#] _____________________________ My Blog | My Articles | My Flickr | WinMacro

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups