Passing a method as a parameter

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hardieca@hotmail.com

    Passing a method as a parameter

    Hello,

    I'm creating a toolbox of useful functions, and I've added a function
    that recursively drills down through a file system searching for files.
    I've created a number of variations of this function, and I imagine
    I'll continue to have use for it.

    What I would like to do is create a version that will take a method as
    a parameter and invoke it as it traverses my file system path. Here is
    some pseudo-code that will hopefully illustrate what I'm trying to do:

    public class Toolbox{
    public void (string startDirectory, Method myMethod) fileDriller{
    foreach (string f in Directory.GetFi les(startDirect ory){
    myMethod(f);
    }
    foreach (string d in Directory.GetDi rectories(start Directory){
    fileDriller(d, myMethod);
    }
    }
    }

    So let's say I wanted to determine the length of each filename in a
    directory of interest and every subdirectory:

    public void lengthOfFileNam e(string fileName){
    Console.write(f ileName.Length. toString());
    }

    static void Main(){
    Toolbox tb = new Toolbox();
    tb.fileDriller( "C:\\somedi r", lengthOfFileNam e);
    }

    I tried passing a MethodInfo object, but I'm running into issues
    because, I believe, it's an abstract class. If anyone has any idea how
    I would accomplish this, please let me know!

    Regards,

    Chris

  • Dustin Campbell

    #2
    Re: Passing a method as a parameter

    If the signature of your method is known, use a delegate:

    public void delegate FileMethod(stri ng fileName);

    public void FileDriller(str ing startDirectory, FileMethod myMethod)
    {
    foreach (string f in Directory.GetFi les(startDirect ory))
    myMethod(f);

    foreach (string d in Directory.GetDi rectories(start Directory))
    FileDriller(d, myMethod);
    }

    Dustin Campbell
    Developer Express Inc.


    Comment

    • Chris Fulstow

      #3
      Re: Passing a method as a parameter

      You could do this using delegates, or using the "strategy" design
      pattern.

      Delegates


      Strategy pattern
      Learn how to use the C# Strategy design pattern to define a family of algorithms, encapsulates each one, and makes them interchangeable, with quick and easy examples. 100% Source code.


      Good luck!

      hardi...@hotmai l.com wrote:
      Hello,
      >
      I'm creating a toolbox of useful functions, and I've added a function
      that recursively drills down through a file system searching for files.
      I've created a number of variations of this function, and I imagine
      I'll continue to have use for it.
      >
      What I would like to do is create a version that will take a method as
      a parameter and invoke it as it traverses my file system path. Here is
      some pseudo-code that will hopefully illustrate what I'm trying to do:
      >
      public class Toolbox{
      public void (string startDirectory, Method myMethod) fileDriller{
      foreach (string f in Directory.GetFi les(startDirect ory){
      myMethod(f);
      }
      foreach (string d in Directory.GetDi rectories(start Directory){
      fileDriller(d, myMethod);
      }
      }
      }
      >
      So let's say I wanted to determine the length of each filename in a
      directory of interest and every subdirectory:
      >
      public void lengthOfFileNam e(string fileName){
      Console.write(f ileName.Length. toString());
      }
      >
      static void Main(){
      Toolbox tb = new Toolbox();
      tb.fileDriller( "C:\\somedi r", lengthOfFileNam e);
      }
      >
      I tried passing a MethodInfo object, but I'm running into issues
      because, I believe, it's an abstract class. If anyone has any idea how
      I would accomplish this, please let me know!
      >
      Regards,
      >
      Chris

      Comment

      Working...