Delegate.CreateDelegate equivalent in NetCF.

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

    Delegate.CreateDelegate equivalent in NetCF.

    Hi,

    I need Delegate.Create Delegate method equivalent in compact
    framework.

    Best regards,
    Klaudiusz
  • Klaudiusz Bryja

    #2
    Re: Delegate.Create Delegate equivalent in NetCF.

    On 24 Lip, 12:14, Klaudiusz Bryja <bryja_klaudi.. .@poczta.fmwrot e:
    Hi,
    >
    I need Delegate.Create Delegate method equivalent in compact
    framework.
    >
    Best regards,
    Klaudiusz
    Sorry I send before finish. In full framework this method create
    delegate of proper type (e.g. KeyEventHandler , EventHandler).

    Comment

    • Marc Gravell

      #3
      Re: Delegate.Create Delegate equivalent in NetCF.

      It is there in 3.5; in 2.0 you can invoke the method (etc) directly via
      Invoke, or if you absolutely *need* a delegate perhaps use an anonymous
      method (and capture) to create it, i.e.

      (where getter is the instance of a Getter delegate - think Func<,>)

      #if CF2
      getter = delegate(TEntit y instance) {
      return (TValue)propert y.GetValue(inst ance, null);
      };
      #else
      getter = (Getter)Delegat e.CreateDelegat e(
      typeof(Getter), null, method);
      #endif

      A bit hacky, but it should work. Actually, in my version I just use
      Invoke/GetValue etc directly, but you might have different needs...


      Marc

      Comment

      • Klaudiusz Bryja

        #4
        Re: Delegate.Create Delegate equivalent in NetCF.

        On 24 Lip, 12:14, Klaudiusz Bryja <bryja_klaudi.. .@poczta.fmwrot e:
        Hi,
        >
        I need Delegate.Create Delegate method equivalent in compact
        framework.
        >
        Best regards,
        Klaudiusz
        Sorry i send to fast. I have solution like this but it create delegate
        EventHandler type only. I try to use reflection to call constructor
        (e.g. do something like Deleagte a = new SomeEventHandle r()) but I can
        get target method address. Meybe you have something which create
        Delegate of any type.

        Comment

        • Klaudiusz Bryja

          #5
          Re: Delegate.Create Delegate equivalent in NetCF.

          On 24 Lip, 12:35, Marc Gravell <marc.grav...@g mail.comwrote:
          It is there in 3.5; in 2.0 you can invoke the method (etc) directly via
          Invoke, or if you absolutely *need* a delegate perhaps use an anonymous
          method (and capture) to create it, i.e.
          >
          (where getter is the instance of a Getter delegate - think Func<,>)
          >
          #if CF2
          getter = delegate(TEntit y instance) {
          return (TValue)propert y.GetValue(inst ance, null);
          };
          #else
          getter = (Getter)Delegat e.CreateDelegat e(
          typeof(Getter), null, method);
          #endif
          >
          A bit hacky, but it should work. Actually, in my version I just use
          Invoke/GetValue etc directly, but you might have different needs...
          >
          Marc
          Hi,

          I can't use NetCF 3.5. I can use only 2.0.

          Comment

          • Marc Gravell

            #6
            Re: Delegate.Create Delegate equivalent in NetCF.

            Here's a CF2 example with a button and a textbox, linked dynamically on
            name (i.e. in the scenario where we can't just use += etc). It isn't as
            elegant as we might like, but it works:

            public Form1()
            {
            InitializeCompo nent();

            // hook "Click" of button to "SomeMethod "
            MethodInfo method = GetType().GetMe thod("SomeMetho d");
            EventHandler handler = delegate(object s, EventArgs e)
            {
            method.Invoke(t his, new object[] { s, e });
            };
            button1.GetType ().GetEvent("Cl ick")
            .AddEventHandle r(button1, handler);
            }
            public void SomeMethod(obje ct sender, EventArgs args)
            {
            label1.Text = "it worked";
            }

            Comment

            • Marc Gravell

              #7
              Re: Delegate.Create Delegate equivalent in NetCF.

              Yes; my example was *for* CF 2.0. In CF 3.5 you just use
              Delegate.Create Delegate. See the other exaple I've just posted.

              Marc

              Comment

              • Klaudiusz Bryja

                #8
                Re: Delegate.Create Delegate equivalent in NetCF.

                On 24 Lip, 12:44, Marc Gravell <marc.grav...@g mail.comwrote:
                Here's a CF2 example with a button and a textbox, linked dynamically on
                name (i.e. in the scenario where we can't just use += etc). It isn't as
                elegant as we might like, but it works:
                >
                public Form1()
                {
                InitializeCompo nent();
                >
                // hook "Click" of button to "SomeMethod "
                MethodInfo method = GetType().GetMe thod("SomeMetho d");
                EventHandler handler = delegate(object s, EventArgs e)
                {
                method.Invoke(t his, new object[] { s, e });
                };
                button1.GetType ().GetEvent("Cl ick")
                .AddEventHandle r(button1, handler);
                }
                public void SomeMethod(obje ct sender, EventArgs args)
                {
                label1.Text = "it worked";
                }
                Hi,

                I do like you and it's work. My question was - how to create delegate
                having eventhandlertyp e. In you example you know the type of delegate
                (EventHandler) but I have this information in GetEvent("name of
                event").EvenatH andlerType.

                Try you example with KeyDown event. You should get argumentexcepti on
                because you create EventHandler but you should create KeyEventHandler .
                I want solution which create delegate and I said it has to be
                KeyEventHandler type or MouseEventHandl er or something else.

                Comment

                • Klaudiusz Bryja

                  #9
                  Re: Delegate.Create Delegate equivalent in NetCF.

                  On 24 Lip, 12:35, Marc Gravell <marc.grav...@g mail.comwrote:
                  It is there in 3.5; in 2.0 you can invoke the method (etc) directly via
                  Invoke, or if you absolutely *need* a delegate perhaps use an anonymous
                  method (and capture) to create it, i.e.
                  >
                  (where getter is the instance of a Getter delegate - think Func<,>)
                  >
                  #if CF2
                  getter = delegate(TEntit y instance) {
                  return (TValue)propert y.GetValue(inst ance, null);
                  };
                  #else
                  getter = (Getter)Delegat e.CreateDelegat e(
                  typeof(Getter), null, method);
                  #endif
                  >
                  A bit hacky, but it should work. Actually, in my version I just use
                  Invoke/GetValue etc directly, but you might have different needs...
                  >
                  Marc
                  Hi Marc,
                  I have question for you. You said that use CF2 but I haven't TEntity
                  and TValue types. What is it (I search information but I get something
                  connected to LINQ)? What is "property" object?

                  Comment

                  • Marc Gravell

                    #10
                    Re: Delegate.Create Delegate equivalent in NetCF.

                    I'm sorry, I didn't do a very good job of explaining...

                    That code was copy/paste from some existing code that uses generics;
                    TEntity / TValue are the generic types of the class and property-type
                    (respectively), and "property" is the PropertyInfo - i.e. here:

                    public class Foo {
                    public int Bar {set;set;}
                    }

                    TEntity is Foo, TValue is int, and property is Bar... basically that
                    code is from some code that manages data access to an entity (as part of
                    a serialization/deserialization routine).

                    Marc

                    Comment

                    Working...