Variable Argument Generic Delegate

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Advait Mohan Raut

    #1

    Variable Argument Generic Delegate

    Hello,
    I am using VC# 2005 ; C# 2.0 I am working on the performance
    measurement tool. For that I need my code to call user defined method
    along with its parameters. For that should I use a generic delegate
    with variable length argument or something else ? if it is then how to
    use it ?

    AA
    advait
  • Peter Duniho

    #2
    Re: Variable Argument Generic Delegate

    On Sat, 20 Sep 2008 05:20:14 -0700, Advait Mohan Raut
    <advait_raut@in diatimes.comwro te:
    Hello,
    I am using VC# 2005 ; C# 2.0 I am working on the performance
    measurement tool. For that I need my code to call user defined method
    along with its parameters. For that should I use a generic delegate
    with variable length argument or something else ? if it is then how to
    use it ?
    Careful with that word "generic". It has a very specific meaning in C#,
    and there's nothing in your post that suggests it's applicable here.

    As far as the actual question goes, the Delegate class has the
    DynamicInvoke() method, which takes a variable-length parameter list. So,
    all delegate instances have this method that can be used when you don't
    have compile-time knowledge of the signature of the method.

    If that doesn't meet your needs, you'll have to be more specific about
    your question, explaining why DynamicInvoke() doesn't work for you and
    what exact behavior you think you need.

    Pete

    Comment

    • Advait Mohan Raut

      #3
      Re: Variable Argument Generic Delegate



      Peter Duniho wrote:
      Careful with that word "generic". It has a very specific meaning in C#,
      and there's nothing in your post that suggests it's applicable here.
      >
      As far as the actual question goes, the Delegate class has the
      DynamicInvoke() method, which takes a variable-length parameter list. So,
      all delegate instances have this method that can be used when you don't
      have compile-time knowledge of the signature of the method.
      >
      If that doesn't meet your needs, you'll have to be more specific about
      your question, explaining why DynamicInvoke() doesn't work for you and
      what exact behavior you think you need.
      >
      Pete
      Hello,
      My class accepts a delegate from the programmer. If I make my class to
      accept particual type of delegate then it will create problem when the
      methos of diffrent signature is to be passed. If I make my function
      just to accept general 'Delegate' then the problem is to create the
      instance of the delegate which I would pass.

      class TestClass
      {
      publicTestClass (Delegate d)
      {
      del=d;
      }
      public Call(params object[] args)
      {
      del.InvokeDynam ic(args);
      }
      private Delegate del;
      }

      int MyMethod(int n)
      {
      ....
      }

      //Main()

      TestClass test=new TestClass(???); // <--- The problem is here to
      pass MyMethod


      AA
      Advait

      Comment

      • Peter Duniho

        #4
        Re: Variable Argument Generic Delegate

        On Sat, 20 Sep 2008 13:09:15 -0700, Advait Mohan Raut
        <advait_raut@in diatimes.comwro te:
        [...]
        TestClass test=new TestClass(???); // <--- The problem is here to
        pass MyMethod
        Just pass an instance of a concrete delegate created with your method.

        You can't make it simply an instance of "Delegate", since that's an
        abstract class. But any other compatible delegate type will do.

        If you're using .NET 3.0, you could use the Func<generic delegate type
        in this particular example (and numerous other similar ones):

        TestClass test = new TestClass((Func <int, int>)MyMethod);

        Otherwise, you could just declare your own delegate type and use that (you
        could even make it generic, just like .NET's own Func<type).

        The Action<generic delegate type would be useful for methods that return
        void.

        I find it intriguing that C# still cannot infer an appropriate anonymous
        delegate type to use in this case. It knows the signature, and it knows
        the Delegate type is special. If it could, it would fix these kinds of
        scenarios by making them more elegant, including one very notable one: the
        Control.Invoke( ) method.

        But for now, you need to be explicit about what type you really want (on
        the bright side, once you've defined the precise type, the compiler will
        at least infer the correct object instantiation for you :) ).

        Pete

        Comment

        • Advait Mohan Raut

          #5
          Re: Variable Argument Generic Delegate

          On Sep 21, 6:33 am, "Peter Duniho" <NpOeStPe...@nn owslpianmk.com>
          wrote:
          On Sat, 20 Sep 2008 13:09:15 -0700, Advait Mohan Raut  
          >
          <advait_r...@in diatimes.comwro te:
          [...]
          TestClass test=new TestClass(???);  // <--- The problem is here to
          pass MyMethod
          >
          Just pass an instance of a concrete delegate created with your method.
          >
          You can't make it simply an instance of "Delegate", since that's an  
          abstract class.  But any other compatible delegate type will do.
          >
          If you're using .NET 3.0, you could use the Func<generic delegate type  
          in this particular example (and numerous other similar ones):
          >
               TestClass test = new TestClass((Func <int, int>)MyMethod);
          >
          Otherwise, you could just declare your own delegate type and use that (you  
          could even make it generic, just like .NET's own Func<type).
          >
          The Action<generic delegate type would be useful for methods that return  
          void.
          >
          I find it intriguing that C# still cannot infer an appropriate anonymous  
          delegate type to use in this case.  It knows the signature, and it knows  
          the Delegate type is special.  If it could, it would fix these kinds of 
          scenarios by making them more elegant, including one very notable one: the  
          Control.Invoke( ) method.
          >
          But for now, you need to be explicit about what type you really want (on  
          the bright side, once you've defined the precise type, the compiler will  
          at least infer the correct object instantiation for you :) ).
          >
          Pete
          Thank you Peter Duniho for the response.
          Since I use C# 2.0 , I don't have any other way except creating the
          signatures for the functions.

          Comment

          • Peter Duniho

            #6
            Re: Variable Argument Generic Delegate

            On Sun, 21 Sep 2008 10:57:10 -0700, Advait Mohan Raut
            <advait_raut@in diatimes.comwro te:
            Thank you Peter Duniho for the response.
            Since I use C# 2.0 , I don't have any other way except creating the
            signatures for the functions.
            Note that using C# 2.0 isn't necessarily mutually exclusive with using
            ..NET 3.0. Though, the default toolset does make it that way.

            In any case, like I said, you can declare the generic delegate types
            yourself. There's nothing special about them. They just happen to be
            built into .NET. So you don't necessarily need to declare delegates for
            every possible signature. You can just declare the needed generic
            delegates and use them as appropriate:

            delegate void Action();
            delegate void Action<T>(T t);
            delegate void Action<T1, T2>(T1 t1, T2 t2);
            // etc.
            delegate TResult Func<TResult>() ;
            delegate TResult Func<T, TResult>(T t);
            delegate TResult Func<T1, T2, TResult>(T1 t1, T2 t2);
            // etc.

            Pete

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Variable Argument Generic Delegate

              Peter Duniho <NpOeStPeAdM@nn owslpianmk.comw rote:
              If you're using .NET 3.0, you could use the Func<generic delegate type
              in this particular example (and numerous other similar ones):
              >
              TestClass test = new TestClass((Func <int, int>)MyMethod);
              >
              Otherwise, you could just declare your own delegate type and use that (you
              could even make it generic, just like .NET's own Func<type).
              >
              The Action<generic delegate type would be useful for methods that return
              void.
              Just a slight correction - these types were introduced in .NET 3.5, not
              ..NET 3.0.

              --
              Jon Skeet - <skeet@pobox.co m>
              Web site: http://www.pobox.com/~skeet
              Blog: http://www.msmvps.com/jon.skeet
              C# in Depth: http://csharpindepth.com

              Comment

              • Peter Duniho

                #8
                Re: Variable Argument Generic Delegate

                On Sun, 21 Sep 2008 22:47:32 -0700, Jon Skeet [C# MVP] <skeet@pobox.co m>
                wrote:
                Just a slight correction - these types were introduced in .NET 3.5, not
                .NET 3.0.
                Yeah, I always get that mixed up. Especially since Action<Thas been
                with us since .NET 2.0, it's hard to keep track.

                Oh well...either way, it's probably easier to just define them oneself
                than to mess around with getting C# 2.0 working with .NET 3.5. :)

                Comment

                • Marc Gravell

                  #9
                  Re: Variable Argument Generic Delegate

                  I find it intriguing that C# still cannot infer an appropriate anonymous
                  delegate type to use in this case
                  ...
                  including one very notable one: the
                  Control.Invoke( ) method.
                  So true; a real frustration at times. As an aside (and not helpful for
                  Advait with C# 2.0), one option with Control.Invoke is an extension
                  method:

                  public static void Invoke(this Control control, Action action)
                  { // note: could use MethodInvoker for 2.0 compartibility
                  (with ExtensionAttrib ute)
                  if (control == null) throw new
                  ArgumentNullExc eption("control ");
                  if (action == null) throw new
                  ArgumentNullExc eption("action" );
                  control.Invoke( action, null);
                  }

                  then you can use:

                  this.Invoke(() =this.Text = "Hi");

                  Marc

                  Comment

                  Working...