Blog

Intelligent objects

When I was working on the code folding patch to MonoDevelop, I had curious problem. I wished that the buffer returned only the code to everything and the folded version to some internal methods I used.

The problem could only be solved implementing an internal method that I invoked to return the folded version. Now, I found a new way, that not only solves my problem but also introduces a concept of Intelligent Objects – objects that behave according to the context, or the callers if you will.

Pretty simple:

System.Diagnostics.StackTrace stack = new System.Diagnostics.StackTrace (true);
System.Diagnostics.StackFrame stackf = new System.Diagnostics.StackFrame (1);
if (stackf.GetMethod ().DeclaringType == typeof (ABC))
{ ... } else { ...}

This code analyses the stack thus introducing the concept I wanted, to behave according to the caller.

This can be even more useful in other cases, for instance, singleton classes that need to pass the ‘master’ object to each child – using this we can skip that requirement and access directly the factory that created the object and avoid all the extra code the usual approach involves.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Leave a Reply

For spam filtering purposes, please copy the number 4653 to the field below: