How to register a property to event ?

G

Guest

Is there a way to have e delegate for property instead of method ?

For method like this:

void doSomthing(int val);

We can have a delegate like: public delegate void dodo(int val);

and event fro it in the shape of: public event dodo dodoHandler;

and then the above method can be register to the event like this:

dodo += doSomthing;


But in my case I want to register to event using property in the form of:

int m_filed;
public int Filed
{
get { return filed; }
set { filed = value; }
}


Can I do that and How ?
 
M

Marc Gravell

No; and what would you have the property do (on invoke) anyway? properties
are really a pair of methods; a get and a set. The Set would match your
delegate.

Event delegates can technically follow any pattern, but the void
SomeHandler(object, SomeEventArgs : EventArgs) pattern (or the
EventHandler<T> where T : EventArgs pattern) should usually be followed.

Using your unusual pattern, you could use 2.0 delegates to do:

object.SomeEvent += delegate (int val) {
someOtherObject.SomeProperty += val;
};

But I don't think it is a good idea.

What are you actually trying to achieve? This might steer the best answer...
For instance - is this for property-change notifications? or what?

Marc
 
G

Guest

What I'm actually trying to achieve is:

I already have a property to my field.

Some other class in the application has a delegate/event (which I'm writing).

I need to register some function to this delegate to set my filed, but my
filed already has a the SET function inside the property of his. And I want
to avoid writing another Set method just for this event.

How can I do that?
 
M

Marc Gravell

Then I would use the 2.0 inline delegate syntax, but using the standard
event pattern. Depending on the scenario, the new value could be either in
the event-args, or back on the sender (typical for a SomethingChanged
event), i.e.

// EventHandler example
someInstance.SomethingChanged += delegate {
someOtherInstancePossiblyThis.SomeProperty = someInstance.Something;
};
or
// SomeEventHandler example where SomeEventArgs has a .SomeValue property
someInstance.SomeEvent += delegate (object sender, SomeEventArgs args) {
someOtherInstancePossiblyThis.SomeProperty = args.SomeValue;
};

(note that in either case you might also case "sender" instead of using
someOtherInstancePossiblyThis)

Marc
 
J

Jon Skeet [C# MVP]

Sharon said:
What I'm actually trying to achieve is:

I already have a property to my field.

Some other class in the application has a delegate/event (which I'm writing).

I need to register some function to this delegate to set my filed, but my
filed already has a the SET function inside the property of his. And I want
to avoid writing another Set method just for this event.

How can I do that?

You can use reflection to set it up. A complete example is below. It's
pretty longwinded, but if you needed this on a regular basis you could
easily set it up in a helper method - especially with generics, if
you're using 2.0.


using System;
using System.Reflection;

delegate void SettingHandler (string value);

class PropertyClass
{
string name;

public string Name
{
get { return name; }
set { name = value; }
}
}

class EventClass
{
public event SettingHandler SetEvent;

public void RaiseEvent (string value)
{
SetEvent(value);
}
}

class Test
{
static void Main()
{
PropertyClass pc = new PropertyClass();

EventClass ec = new EventClass();

PropertyInfo prop = typeof(PropertyClass).GetProperty("Name");
MethodInfo method = prop.GetSetMethod();

SettingHandler handler = (SettingHandler)
Delegate.CreateDelegate
(typeof(SettingHandler), pc, method);

ec.SetEvent += handler;

ec.RaiseEvent ("testing");

Console.WriteLine (pc.Name);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top