Delegate.CreateDelegate equivalent in NetCF.

  • Thread starter Thread starter Klaudiusz Bryja
  • Start date Start date
K

Klaudiusz Bryja

Hi,

I need Delegate.CreateDelegate method equivalent in compact
framework.

Best regards,
Klaudiusz
 
Hi,

I need Delegate.CreateDelegate 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).
 
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(TEntity instance) {
return (TValue)property.GetValue(instance, null);
};
#else
getter = (Getter)Delegate.CreateDelegate(
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 need Delegate.CreateDelegate 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 SomeEventHandler()) but I can
get target method address. Meybe you have something which create
Delegate of any type.
 
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(TEntity instance) {
return (TValue)property.GetValue(instance, null);
};
#else
getter = (Getter)Delegate.CreateDelegate(
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.
 
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()
{
InitializeComponent();

// hook "Click" of button to "SomeMethod"
MethodInfo method = GetType().GetMethod("SomeMethod");
EventHandler handler = delegate(object s, EventArgs e)
{
method.Invoke(this, new object[] { s, e });
};
button1.GetType().GetEvent("Click")
.AddEventHandler(button1, handler);
}
public void SomeMethod(object sender, EventArgs args)
{
label1.Text = "it worked";
}
 
Yes; my example was *for* CF 2.0. In CF 3.5 you just use
Delegate.CreateDelegate. See the other exaple I've just posted.

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:

public Form1()
{
InitializeComponent();

// hook "Click" of button to "SomeMethod"
MethodInfo method = GetType().GetMethod("SomeMethod");
EventHandler handler = delegate(object s, EventArgs e)
{
method.Invoke(this, new object[] { s, e });
};
button1.GetType().GetEvent("Click")
.AddEventHandler(button1, handler);
}
public void SomeMethod(object sender, EventArgs args)
{
label1.Text = "it worked";
}

Hi,

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

Try you example with KeyDown event. You should get argumentexception
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 MouseEventHandler or something else.
 
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(TEntity instance) {
return (TValue)property.GetValue(instance, null);
};
#else
getter = (Getter)Delegate.CreateDelegate(
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).

Marc
 
Back
Top