N
Navaneeth.K.N
Hi all,
Recently I found an interesting question on C# forums about clearing event
handlers of an event. I tried to give it a solution, but failed. I am
interested to know how you guys take this. Here it goes
class Product
{
public event EventHandler ProductChanged;
public Product(string name) {
this.name = name;
}
private string name;
public string Name
{
get { return name; }
set {
name = value;
if (ProductChanged != null)
ProductChanged(this, EventArgs.Empty);
}
}
}
"ProductChanged" event will have many subscribers. I need to clear all these
subscribers from outside "Product" class. Reason is, I am not allowed to
change the "Product" class. I am attaching the event handlers like this,
Product first = new Product("First product");
first.ProductChanged += new EventHandler(first_ProductChanged);
Product second = new Product("Second product");
second.ProductChanged += new EventHandler(second_ProductChanged);
// need to clear the ProductChanged event handlers here
// I can't get the invocation list of "Product.ProductChanged" event here.
is there anyway to achieve this? I think some kind of reflection can do
that, but I am not sure how to go about it.
Any help would be great
Recently I found an interesting question on C# forums about clearing event
handlers of an event. I tried to give it a solution, but failed. I am
interested to know how you guys take this. Here it goes
class Product
{
public event EventHandler ProductChanged;
public Product(string name) {
this.name = name;
}
private string name;
public string Name
{
get { return name; }
set {
name = value;
if (ProductChanged != null)
ProductChanged(this, EventArgs.Empty);
}
}
}
"ProductChanged" event will have many subscribers. I need to clear all these
subscribers from outside "Product" class. Reason is, I am not allowed to
change the "Product" class. I am attaching the event handlers like this,
Product first = new Product("First product");
first.ProductChanged += new EventHandler(first_ProductChanged);
Product second = new Product("Second product");
second.ProductChanged += new EventHandler(second_ProductChanged);
// need to clear the ProductChanged event handlers here
// I can't get the invocation list of "Product.ProductChanged" event here.
is there anyway to achieve this? I think some kind of reflection can do
that, but I am not sure how to go about it.
Any help would be great