Watching for an object to change - ObjectWatcher?

  • Thread starter Thread starter vector
  • Start date Start date
V

vector

I'd like to use something similar to the FileSystemWatcher to monitor
changes to objects in my own code. Something I can hook bitmaps,
strings, anything to, and have it raise an event when the object is
changed, possibly passing along the new hashcode for the object.

I guess it would be possible to write something like that that checks
the hashcode of an event at a specified interval, but that's a nasty
hack. Isn't there a more elegant way to accomplish this?

I envision something like this:

string myString=null;

objectWatcher.AddWatch(myString);

myString="Hello, world!"; //objWatcher would raise an event here
 
I'd like to use something similar to the FileSystemWatcher to monitor
changes to objects in my own code. Something I can hook bitmaps,
strings, anything to, and have it raise an event when the object is
changed, possibly passing along the new hashcode for the object.

I guess it would be possible to write something like that that checks
the hashcode of an event at a specified interval, but that's a nasty
hack. Isn't there a more elegant way to accomplish this?

I envision something like this:

string myString=null;

objectWatcher.AddWatch(myString);

myString="Hello, world!"; //objWatcher would raise an event here

Assignment doesn't change any object at all.
The only Solution would be to let your objects raise an event if a set
property is called and the value is changed
You can put the event is an interface so you can use if for multiple
classes:

interface IWatchable
{
event EventHandler Changed;
}

class MyClass : IWatchable
{
event EventHandler Changed;

string myProperty;

public string MyProperty
{
set
{
if (value!=myProperty){myProperty=value; Changed(this, new
EventArgs());}
}
}

}

But you cannot use this for predefined classes when you don't inherit from
them.
 
Hi,

The only way to implement it would be if each object that can be monitorable
include an event for that, otherwise I don't see a clear way of doing what
you want.


Cheers,
 

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

Back
Top