Indirect method calls ... how to?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gaetan

    Indirect method calls ... how to?

    Is is possible in C# to have the equivalent of an array of function pointers in C?

    I have a situation where a top level class exposes methods like Add, Delete, ... and a few
    child classes with the same methods. Depending on a configuration parameter, the child
    method will be invoked when the top level class is invoked.

    I would like to avoid having to do something like this:

    int configuration;

    public bool Add()
    {
    switch (configuration)
    {
    case 1:
    return ChildClass1.Add ();
    case 2:
    return ChildClass2.Add ();
    }
    }

    I would rather have something like this instead (code not complete)

    Arraylist MethodPTR;
    int configuration;

    public mainClassConstr uctor()
    {
    // Initialize MethodPTR with the address of the methods

    MethodPTR = new ArrayList(8);
    configuration = RetrieveConfigu ration();

    switch (configuration)
    {
    case 1:
    MethodPTR[0] = ChildClass1.Add ; // Assistance required here
    MethodPTR[1] = ChildClass1.Del ete; // Assistance required here
    break;
    case 2:
    MethodPTR[0] = ChildClass2.Add ; // Assistance required here
    MethodPTR[1] = ChildClass2.Del ete; // Assistance required here
    break;
    }
    }

    public bool Add()
    {
    return MedthoPTR[0](); // This is where I need assistance
    }

    public bool Delete()
    {
    return MedthoPTR[1](); // This is where I need assistance
    }

    Thank you all in advance.

    Gaetan



  • C.C. \(aka Me\)

    #2
    Re: Indirect method calls ... how to?

    Not really any function pointers.

    But....

    Take a look at the Reflection classes.

    You can get an object that represents a method (MethodInfo class) from a
    class and then call the Invoke() method of it to actually execute it. You
    could keep an array of MethodInfo objects and treat them sort of like
    function pointers.

    Also, you can Invoke() a method by using a string representation of the
    method name as long as you have an instance of the object that contains it.

    Hope this gives you some ideas!

    --
    ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
    Charles Cox
    VC/VB/C# Developer
    ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~

    "Gaetan" <someone@somewh ere.com> wrote in message
    news:jojst1h964 d2lsg5scjj81co8 r4di9h5ns@4ax.c om...[color=blue]
    > Is is possible in C# to have the equivalent of an array of function
    > pointers in C?
    >
    > I have a situation where a top level class exposes methods like Add,
    > Delete, ... and a few
    > child classes with the same methods. Depending on a configuration
    > parameter, the child
    > method will be invoked when the top level class is invoked.
    >
    > I would like to avoid having to do something like this:
    >
    > int configuration;
    >
    > public bool Add()
    > {
    > switch (configuration)
    > {
    > case 1:
    > return ChildClass1.Add ();
    > case 2:
    > return ChildClass2.Add ();
    > }
    > }
    >
    > I would rather have something like this instead (code not complete)
    >
    > Arraylist MethodPTR;
    > int configuration;
    >
    > public mainClassConstr uctor()
    > {
    > // Initialize MethodPTR with the address of the methods
    >
    > MethodPTR = new ArrayList(8);
    > configuration = RetrieveConfigu ration();
    >
    > switch (configuration)
    > {
    > case 1:
    > MethodPTR[0] = ChildClass1.Add ; // Assistance required here
    > MethodPTR[1] = ChildClass1.Del ete; // Assistance required here
    > break;
    > case 2:
    > MethodPTR[0] = ChildClass2.Add ; // Assistance required here
    > MethodPTR[1] = ChildClass2.Del ete; // Assistance required here
    > break;
    > }
    > }
    >
    > public bool Add()
    > {
    > return MedthoPTR[0](); // This is where I need assistance
    > }
    >
    > public bool Delete()
    > {
    > return MedthoPTR[1](); // This is where I need assistance
    > }
    >
    > Thank you all in advance.
    >
    > Gaetan
    >
    >
    >[/color]


    Comment

    • Peter Rilling

      #3
      Re: Indirect method calls ... how to?

      Well, first some comments than an answer to your questions.

      1) Not really sure if your desired way is any better than the way you are
      rejecting. You are still using a switch statement, you are just moving it
      to a new location.
      2) People who work on this after you may not understand what you are doing.
      You would need to document this really well.
      3) Although not a big issue, your way would not be as performant because
      you are storing pointers to the methods. There would also be the overhead
      of calling the methods. (again this is not a big deal, just thought I would
      mention it.)

      Now, for your answer. The .NET equivalent of function pointers are
      Delegates. When you create a delegate, you tell it what method it should
      wrap. You can then store the delegate for later use.

      On a side note, let me offer some design comments (knowing full well that I
      do not have knowledge of what you are trying to accomplish).

      When you say "child class", are you referring to inherited subclasses? If
      that is the case, then you might want to make Add abstract and then the
      correct derived class method will be called, or you can have the base class
      have an Add method and the derived classes have an AddImpl method incase you
      need the base class to do some additional work. I might be able to offer a
      better design if you wanted to share the relationship between the various
      classes (i.e. inheritance, composition, etc.).



      "Gaetan" <someone@somewh ere.com> wrote in message
      news:jojst1h964 d2lsg5scjj81co8 r4di9h5ns@4ax.c om...[color=blue]
      > Is is possible in C# to have the equivalent of an array of function
      > pointers in C?
      >
      > I have a situation where a top level class exposes methods like Add,
      > Delete, ... and a few
      > child classes with the same methods. Depending on a configuration
      > parameter, the child
      > method will be invoked when the top level class is invoked.
      >
      > I would like to avoid having to do something like this:
      >
      > int configuration;
      >
      > public bool Add()
      > {
      > switch (configuration)
      > {
      > case 1:
      > return ChildClass1.Add ();
      > case 2:
      > return ChildClass2.Add ();
      > }
      > }
      >
      > I would rather have something like this instead (code not complete)
      >
      > Arraylist MethodPTR;
      > int configuration;
      >
      > public mainClassConstr uctor()
      > {
      > // Initialize MethodPTR with the address of the methods
      >
      > MethodPTR = new ArrayList(8);
      > configuration = RetrieveConfigu ration();
      >
      > switch (configuration)
      > {
      > case 1:
      > MethodPTR[0] = ChildClass1.Add ; // Assistance required here
      > MethodPTR[1] = ChildClass1.Del ete; // Assistance required here
      > break;
      > case 2:
      > MethodPTR[0] = ChildClass2.Add ; // Assistance required here
      > MethodPTR[1] = ChildClass2.Del ete; // Assistance required here
      > break;
      > }
      > }
      >
      > public bool Add()
      > {
      > return MedthoPTR[0](); // This is where I need assistance
      > }
      >
      > public bool Delete()
      > {
      > return MedthoPTR[1](); // This is where I need assistance
      > }
      >
      > Thank you all in advance.
      >
      > Gaetan
      >
      >
      >[/color]


      Comment

      • Kevin Spencer

        #4
        Re: Indirect method calls ... how to?

        Check out .Net delegates. This is the equivalent of managed function
        pointers.

        --
        HTH,

        Kevin Spencer
        Microsoft MVP
        ..Net Developer
        Who is Mighty Abbott?
        A twin turret scalawag.

        "Gaetan" <someone@somewh ere.com> wrote in message
        news:jojst1h964 d2lsg5scjj81co8 r4di9h5ns@4ax.c om...[color=blue]
        > Is is possible in C# to have the equivalent of an array of function
        > pointers in C?
        >
        > I have a situation where a top level class exposes methods like Add,
        > Delete, ... and a few
        > child classes with the same methods. Depending on a configuration
        > parameter, the child
        > method will be invoked when the top level class is invoked.
        >
        > I would like to avoid having to do something like this:
        >
        > int configuration;
        >
        > public bool Add()
        > {
        > switch (configuration)
        > {
        > case 1:
        > return ChildClass1.Add ();
        > case 2:
        > return ChildClass2.Add ();
        > }
        > }
        >
        > I would rather have something like this instead (code not complete)
        >
        > Arraylist MethodPTR;
        > int configuration;
        >
        > public mainClassConstr uctor()
        > {
        > // Initialize MethodPTR with the address of the methods
        >
        > MethodPTR = new ArrayList(8);
        > configuration = RetrieveConfigu ration();
        >
        > switch (configuration)
        > {
        > case 1:
        > MethodPTR[0] = ChildClass1.Add ; // Assistance required here
        > MethodPTR[1] = ChildClass1.Del ete; // Assistance required here
        > break;
        > case 2:
        > MethodPTR[0] = ChildClass2.Add ; // Assistance required here
        > MethodPTR[1] = ChildClass2.Del ete; // Assistance required here
        > break;
        > }
        > }
        >
        > public bool Add()
        > {
        > return MedthoPTR[0](); // This is where I need assistance
        > }
        >
        > public bool Delete()
        > {
        > return MedthoPTR[1](); // This is where I need assistance
        > }
        >
        > Thank you all in advance.
        >
        > Gaetan
        >
        >
        >[/color]


        Comment

        • Peter Rilling

          #5
          Re: Indirect method calls ... how to?

          Delegates are "function pointers" in .NET.

          "C.C. (aka Me)" <me@home.com> wrote in message
          news:jZOdnfmBa4 lBykPeRVn-tw@comcast.com. ..[color=blue]
          > Not really any function pointers.
          >
          > But....
          >
          > Take a look at the Reflection classes.
          >
          > You can get an object that represents a method (MethodInfo class) from a
          > class and then call the Invoke() method of it to actually execute it. You
          > could keep an array of MethodInfo objects and treat them sort of like
          > function pointers.
          >
          > Also, you can Invoke() a method by using a string representation of the
          > method name as long as you have an instance of the object that contains
          > it.
          >
          > Hope this gives you some ideas!
          >
          > --
          > ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
          > Charles Cox
          > VC/VB/C# Developer
          > ~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~
          >
          > "Gaetan" <someone@somewh ere.com> wrote in message
          > news:jojst1h964 d2lsg5scjj81co8 r4di9h5ns@4ax.c om...[color=green]
          >> Is is possible in C# to have the equivalent of an array of function
          >> pointers in C?
          >>
          >> I have a situation where a top level class exposes methods like Add,
          >> Delete, ... and a few
          >> child classes with the same methods. Depending on a configuration
          >> parameter, the child
          >> method will be invoked when the top level class is invoked.
          >>
          >> I would like to avoid having to do something like this:
          >>
          >> int configuration;
          >>
          >> public bool Add()
          >> {
          >> switch (configuration)
          >> {
          >> case 1:
          >> return ChildClass1.Add ();
          >> case 2:
          >> return ChildClass2.Add ();
          >> }
          >> }
          >>
          >> I would rather have something like this instead (code not complete)
          >>
          >> Arraylist MethodPTR;
          >> int configuration;
          >>
          >> public mainClassConstr uctor()
          >> {
          >> // Initialize MethodPTR with the address of the methods
          >>
          >> MethodPTR = new ArrayList(8);
          >> configuration = RetrieveConfigu ration();
          >>
          >> switch (configuration)
          >> {
          >> case 1:
          >> MethodPTR[0] = ChildClass1.Add ; // Assistance required here
          >> MethodPTR[1] = ChildClass1.Del ete; // Assistance required here
          >> break;
          >> case 2:
          >> MethodPTR[0] = ChildClass2.Add ; // Assistance required here
          >> MethodPTR[1] = ChildClass2.Del ete; // Assistance required here
          >> break;
          >> }
          >> }
          >>
          >> public bool Add()
          >> {
          >> return MedthoPTR[0](); // This is where I need assistance
          >> }
          >>
          >> public bool Delete()
          >> {
          >> return MedthoPTR[1](); // This is where I need assistance
          >> }
          >>
          >> Thank you all in advance.
          >>
          >> Gaetan
          >>
          >>
          >>[/color]
          >
          >[/color]


          Comment

          • chris martin

            #6
            Re: Indirect method calls ... how to?

            > Is is possible in C# to have the equivalent of an array of function[color=blue]
            > pointers in C?
            >
            > I have a situation where a top level class exposes methods like Add,
            > Delete, ... and a few child classes with the same methods. Depending
            > on a configuration parameter, the child method will be invoked when
            > the top level class is invoked.
            >[/color]

            I would use delegates for this. The following example is not very elegant
            but, it shows the basic use of delegates and from what I read, it should
            apply to your situation.

            Chris

            ---

            class Class1
            {

            [STAThread]
            static void Main(string[] args)
            {
            Class1 class1 = new Class1(0);
            Class1 class2 = new Class1(1);

            class1.Add();
            class1.Delete() ;

            class2.Add();
            class2.Delete() ;

            Console.ReadLin e();
            }

            public delegate void TakeCarOfStuffH andler();

            TakeCarOfStuffH andler addHandler;
            TakeCarOfStuffH andler deleteHandler;

            public Class1(int i)
            {
            ChildClass1 class1 = new ChildClass1();
            ChildClass2 class2 = new ChildClass2();

            switch(i)
            {
            case 0:
            addHandler = new TakeCarOfStuffH andler(class1.A dd);
            deleteHandler = new TakeCarOfStuffH andler(class1.D elete);
            break;
            case 1:
            addHandler = new TakeCarOfStuffH andler(class2.A dd);
            deleteHandler = new TakeCarOfStuffH andler(class2.D elete);
            break;
            }
            }

            public void Add()
            {
            addHandler();
            }

            public void Delete()
            {
            deleteHandler() ;
            }
            }

            class ChildClass1
            {
            public void Add()
            {
            Console.WriteLi ne("ChildClass1 .Add()");
            }

            public void Delete()
            {
            Console.WriteLi ne("ChildClass1 .Delete()");
            }
            }

            class ChildClass2
            {
            public void Add()
            {
            Console.WriteLi ne("ChildClass2 .Add()");
            }

            public void Delete()
            {
            Console.WriteLi ne("ChildClass2 .Delete()");
            }
            }


            Comment

            • Bruce Wood

              #7
              Re: Indirect method calls ... how to?

              I don't understand how your problem is different from standard
              polymorphism:

              public class Base
              {
              public virtual bool Add() { ... }
              public virtual bool Delete() { ... }
              }

              public class Child : Base
              {
              public override bool Add() { ... }
              public override bool Delete() { ... }
              }

              then

              Base aBase = new Child();
              aBase.Add();

              I'm sure I'm missing something here... how do your requirements differ
              from what's offered by polymorphism?

              Comment

              • Gaetan

                #8
                Re: Indirect method calls ... how to?


                Thanks Peter for your explanations. I have taken a closer look at using either Delegates
                or an abstract class. I think that what I'm trying to do could be more easily implemented
                with a base class and derived classes.


                On Mon, 30 Jan 2006 10:21:42 -0800, "Peter Rilling" <peter@nospam.r illing.net> wrote:
                [color=blue]
                >Well, first some comments than an answer to your questions.
                >
                >1) Not really sure if your desired way is any better than the way you are
                >rejecting. You are still using a switch statement, you are just moving it
                >to a new location.
                >2) People who work on this after you may not understand what you are doing.
                >You would need to document this really well.
                >3) Although not a big issue, your way would not be as performant because
                >you are storing pointers to the methods. There would also be the overhead
                >of calling the methods. (again this is not a big deal, just thought I would
                >mention it.)
                >
                >Now, for your answer. The .NET equivalent of function pointers are
                >Delegates. When you create a delegate, you tell it what method it should
                >wrap. You can then store the delegate for later use.
                >
                >On a side note, let me offer some design comments (knowing full well that I
                >do not have knowledge of what you are trying to accomplish).
                >
                >When you say "child class", are you referring to inherited subclasses? If
                >that is the case, then you might want to make Add abstract and then the
                >correct derived class method will be called, or you can have the base class
                >have an Add method and the derived classes have an AddImpl method incase you
                >need the base class to do some additional work. I might be able to offer a
                >better design if you wanted to share the relationship between the various
                >classes (i.e. inheritance, composition, etc.).
                >[/color]

                Comment

                • Gaetan

                  #9
                  Re: Indirect method calls ... how to?

                  Thanks Bruce ... this is the way I will go.

                  On 30 Jan 2006 10:37:17 -0800, "Bruce Wood" <brucewood@cana da.com> wrote:
                  [color=blue]
                  >I don't understand how your problem is different from standard
                  >polymorphism :
                  >
                  >public class Base
                  >{
                  > public virtual bool Add() { ... }
                  > public virtual bool Delete() { ... }
                  >}
                  >
                  >public class Child : Base
                  >{
                  > public override bool Add() { ... }
                  > public override bool Delete() { ... }
                  >}
                  >
                  >then
                  >
                  >Base aBase = new Child();
                  >aBase.Add();
                  >
                  >I'm sure I'm missing something here... how do your requirements differ
                  >from what's offered by polymorphism?[/color]

                  Comment

                  Working...