问题
I've implemented a class that looks like this interface:
[ImmutableObject(true)]
public interface ICustomEvent
{
void Invoke(object sender, EventArgs e);
ICustomEvent Combine(EventHandler handler);
ICustomEvent Remove(EventHandler handler);
ICustomEvent Combine(ICustomEvent other);
ICustomEvent Remove(ICustomEvent other);
}
This CustomEvent class works much like a MulticastDelegate. It can invoked. It can be combined with another CustomEvent. And a CustomEvent can be removed from another CustomEvent.
Now, I want to declare a class like this:
class EventProvider
{
public event CustomEvent MyEvent;
private void OnMyEvent()
{
var myEvent = this.MyEvent;
if (myEvent != null) myEvent.Invoke(this, EventArgs.Empty);
}
}
Unfortunately, this code does not compile. A Compiler Error CS0066 appears:
'EventProvider.MyEvent': event must be of a delegate type
Basically, what I need is a property that has add and remove accessors instead of get and set. I think the only way to have that is using the event keyword. I know that one obvious alternative is to declare two methods that would do the adding and removing, but I want to avoid that too.
Does anybody knows if there is a nice solution this problem? I wonder if there is any way to cheat the compiler to accept a non-delegate type as an event. A custom attribute, perhaps.
By the way, someone asked a similar question in experts-exchange.com. Since that site is not free, I can't see the responses. Here is the topic: http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_21697455.html
回答1:
If you want to be able to add and remove CustomEvent
objects from the event (instead of regular delegates), there are two options:
Make an implicit cast from ICustomEvent to EventHandler (or some other delegate) that returns an instance method of ICustomEvent (probably Invoke), then use the Target property of the delegate to get the original ICustomEvent in the add
and remove
accessors.
EDIT: Like this:
CustomEvent myEvent;
public event EventHandler MyEvent {
add {
if (value == null) throw new ArgumentNullException("value");
var customHandler = value.Target as ICustomEvent;
if (customHandler != null)
myEvent = myEvent.Combine(customHandler);
else
myEvent = myEvent.Combine(value); //An ordinary delegate
}
remove {
//Similar code
}
}
Note that you'll still need to figure out how to add the first handler if it's a delegate (if the myEvent
field is null
)
Make a writable property of type CustomEvent, then overload the +
and -
operators to allow +=
and -=
on the property.
EDIT: To prevent your callers from overwriting the event, you could expose the previous value in CustomEvent (I'm assuming it works like an immutable stack) and, in the setter, add
if (myEvent.Previous != value && value.Previous != myEvent)
throw new ArgumentException("You cannot reset a CustomEvent", "value");
Note that when the last handler is removed, both value
and myEvent.Previous
will be null
.
回答2:
Try this:
CustomEvent myEvent
public event EventHandler MyEvent {
add { myEvent = myEvent.Combine(value); }
remove {myEvent = myEvent.Remove(value); }
}
You can add and remove normal EventHandler delegates to it, and it will execute the add
and remove
accessors.
EDIT: You can find a weak event implementation here.
2nd EDIT: Or here.
回答3:
What are you trying to accomplish that you can't use delegates/events for?
This screams Reinventing the Square Wheel (bottom of the page), but that could just as well be me not understanding the problem.
回答4:
Why don't you try to use the "+=" and the "-=" operators on your CustomEvent class? You cannot override the "+=" and the "-=" operators directly, but they are evaluated by "+" and "-" operator.
Assignment operators cannot be overloaded, but +=, for example, is evaluated using +, which can be overloaded.
http://msdn.microsoft.com/en-us/library/8edha89s(VS.90).aspx
So instead of having event-like add and remove methods, you can have a field or a property that can be combined by += and -= operators. Besides it encapsulates the combination logic inside your own CustomEvent class.
Carlos Loth.
来源:https://stackoverflow.com/questions/1134206/events-of-a-non-delegate-type