Dynamic function call

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

    Dynamic function call

    Is it possible (as was in VB - CallByName) to call function which name was
    generated.
    Example:

    private static void DS_function()
    {
    }

    private static void FD_function()
    {
    }

    public static void call(string prefix)
    {
    CALLBYNAME(pref ix+"_function() "); //VB - like style
    }

    So is it possible to implement in C# ???

    Thanx


  • Jon Skeet [C# MVP]

    #2
    Re: Dynamic function call

    Tamir Khason <tamir-NOSPAM@tcon-NOSPAM.co.il> wrote:[color=blue]
    > Is it possible (as was in VB - CallByName) to call function which name was
    > generated.
    > Example:
    >
    > private static void DS_function()
    > {
    > }
    >
    > private static void FD_function()
    > {
    > }
    >
    > public static void call(string prefix)
    > {
    > CALLBYNAME(pref ix+"_function() "); //VB - like style
    > }
    >
    > So is it possible to implement in C# ???[/color]

    Use reflection (in particular Type.GetMethod/GetMethods) to get the
    MethodInfo, and then Invoke it.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Tamir Khason

      #3
      Re: Dynamic function call

      Reflection is rather "expensive" method to do this, are there other ways to
      implement...?


      "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
      news:MPG.1a1aaf d289d19247989a7 2@msnews.micros oft.com...[color=blue]
      > Tamir Khason <tamir-NOSPAM@tcon-NOSPAM.co.il> wrote:[color=green]
      > > Is it possible (as was in VB - CallByName) to call function which name[/color][/color]
      was[color=blue][color=green]
      > > generated.
      > > Example:
      > >
      > > private static void DS_function()
      > > {
      > > }
      > >
      > > private static void FD_function()
      > > {
      > > }
      > >
      > > public static void call(string prefix)
      > > {
      > > CALLBYNAME(pref ix+"_function() "); //VB - like style
      > > }
      > >
      > > So is it possible to implement in C# ???[/color]
      >
      > Use reflection (in particular Type.GetMethod/GetMethods) to get the
      > MethodInfo, and then Invoke it.
      >
      > --
      > Jon Skeet - <skeet@pobox.co m>
      > http://www.pobox.com/~skeet
      > If replying to the group, please do not mail me too[/color]


      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: Dynamic function call

        Tamir Khason <tamir-NOSPAM@tcon-NOSPAM.co.il> wrote:[color=blue]
        > Reflection is rather "expensive" method to do this, are there other ways to
        > implement...?[/color]

        No - it's an inherently "expensive" operation, as you're binding at
        runtime rather than compile-time. If you have a limited number of
        calls, you could do it with a switch statement instead:

        switch (prefix)
        {
        case "DS":
        DS_function();
        break;
        // etc
        }

        To be honest though, it's not generally a very nice way of doing
        things.

        --
        Jon Skeet - <skeet@pobox.co m>
        Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

        If replying to the group, please do not mail me too

        Comment

        • Jeffrey Tan[MSFT]

          #5
          Re: Dynamic function call


          Hi Tamir,

          ..Net Reflection provides you ability to dynamically create and invoke types.
          Reflection's design aim is for dynamic invoke or create, so it is fit for
          you.
          While just as Jon said, the enumerate way of solution is not a general way.

          If you still have anything unclear, please feel free to let me know.

          Best regards,
          Jeffrey Tan
          Microsoft Online Partner Support
          Get Secure! - www.microsoft.com/security
          This posting is provided "as is" with no warranties and confers no rights.

          --------------------
          | From: "Tamir Khason" <tamir-NOSPAM@tcon-NOSPAM.co.il>
          | References: <u4gWs4CqDHA.37 32@tk2msftngp13 .phx.gbl>
          <MPG.1a1aafd289 d19247989a72@ms news.microsoft. com>
          | Subject: Re: Dynamic function call
          | Date: Tue, 11 Nov 2003 11:18:49 +0200
          | Lines: 36
          | X-Priority: 3
          | X-MSMail-Priority: Normal
          | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
          | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
          | Message-ID: <eHpilUDqDHA.96 4@TK2MSFTNGP09. phx.gbl>
          | Newsgroups: microsoft.publi c.dotnet.langua ges.csharp
          | NNTP-Posting-Host: 198.211.173.74
          | Path:
          cpmsftngxa06.ph x.gbl!TK2MSFTNG XA06.phx.gbl!TK 2MSFTNGXA05.phx .gbl!TK2MSFTNGP 0
          8.phx.gbl!TK2MS FTNGP09.phx.gbl
          | Xref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.csharp:1983 05
          | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
          |
          | Reflection is rather "expensive" method to do this, are there other ways
          to
          | implement...?
          |
          |
          | "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
          | news:MPG.1a1aaf d289d19247989a7 2@msnews.micros oft.com...
          | > Tamir Khason <tamir-NOSPAM@tcon-NOSPAM.co.il> wrote:
          | > > Is it possible (as was in VB - CallByName) to call function which name
          | was
          | > > generated.
          | > > Example:
          | > >
          | > > private static void DS_function()
          | > > {
          | > > }
          | > >
          | > > private static void FD_function()
          | > > {
          | > > }
          | > >
          | > > public static void call(string prefix)
          | > > {
          | > > CALLBYNAME(pref ix+"_function() "); //VB - like style
          | > > }
          | > >
          | > > So is it possible to implement in C# ???
          | >
          | > Use reflection (in particular Type.GetMethod/GetMethods) to get the
          | > MethodInfo, and then Invoke it.
          | >
          | > --
          | > Jon Skeet - <skeet@pobox.co m>
          | > http://www.pobox.com/~skeet
          | > If replying to the group, please do not mail me too
          |
          |
          |

          Comment

          Working...