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<,>)
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.
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
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:
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.
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?
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).
Comment