c# - A method that executes any time a class property is accessed (get or set)? -


C # - .NET 3.5

I have a family of classes that inherited from the same base class Is of I need a method in the base class (received or set) to use any asset in a tool category at any time. However, I do not want to call the base class to write code in each and every property ... instead, I hope that there is a narrative way to "sync" this activity in the base class.

Adding some spices of necessity, I need to determine the property that the access was made, the value of the property and the name of its kind.

I imagine the imagery solution would be a clever combination of generic, and the reflection I can imagine to make some sort of array of representative behaviors on the runtime, but by re-running the member InfoTo in the producer, Would, will affect its performance. Again, I hope there is a more direct "narrative" way to do this.

Any thoughts are most appreciated!

You can not do it automatically, but you can get free for 95% free of charge. See this is the classic case for Aspect-oriented programming , which has class. Here's how you can solve your problem:

  [Serializable] Public class FieldLogger: OnFieldAccessAspect {public override zero OnGetValue (FieldAccessEventArgs EventArgs) {Console.WriteLine (eventArgs.InstanceTag); Console.light line ("found value!"); Base.OnGetValue (EventArgs); } Public Override Zero OnSetValue (FieldAccessEventArgs eventArgs) {int i = (int?) EventArgs.InstanceTag ?? 0; EventArgs.InstanceTag = i + 1; Console.light line ("set value!"); Base.OnSetValue (EventArgs); } Public override InstanceTagRequest GetInstanceTagRequest () {return New InstanceTagRequest ("logger", new GUID ("4f8a4963-82bf-4d32-8775-42cc3cd119bd"), false); }}  

Now, anything that meets FieldLogger will get the same behavior Prestige!


Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

php - Multiple Select with Explode: only returns the word "Array" -

jquery - SimpleModal Confirm fails to submit form -