May 2006 - Posts
ScottGu
blogged about IIS 7.0 on the MSDN .Net show. He and Bill Staples did an interview on IIS 7.0, which will ship with Windows Vista Beta 2. As Scott said:
IIS 7.0 is a major, major update and improvement to the overall Microsoft web platform stack. See it. You can find it here.
Sometimes when you're inside a method you would like to know which method called it. Or even which object. For instance when you write a TraceListener, it might be pretty cool to make the TraceListener smart enough to find out what object called him and from which method. The StackTrace object in .Net makes this pretty easy. Here's a howto...
First, we iterate through the stackframes that are available on the stack, looking for the one we need. This method returns such a StackFrame. We skip the first StackFrame by starting the variable count at 1, because this method is always called from the method GetCallingMethod() (see below).
private StackFrame GetCallingStackFrame()
{
StackTrace stack;
StackFrame stackFrame;
stack = new StackTrace(true);
// Initialize the StackFrame by creating a default one.
stackFrame = new StackFrame("ErrorDeterminingStackFrame", 0, 0);
for (int count = 1; count < stack.FrameCount; count++)
{
// At my current project I skipped the StackFrame if the calling method was 'WriteLine', because that's the WriteLine method in the TraceListener.
if (!stack.GetFrame(count).GetMethod().Name.Equals("WriteLine"))
{
stackFrame = stack.GetFrame(count);
break;
}
}
return stackFrame;
}
Next, we can get our information from the StackFrame. There's lots of stuff on there, like the method, the filename and even line- and columnnumbers. The method below returns a string containing CallingObject.CallingMethod.
protected string GetCallingMethod()
{
string methodName;
StackFrame stackFrame;
stackFrame = GetCallingStackFrame();
methodName = stackFrame.GetMethod().DeclaringType.Name;
methodName += "." + stackFrame.GetMethod().Name;
return methodName;
}
I know there's some work in this code when it comes to defensive programming. For instance: who tells me GetMethod() will return anything containing a DeclaringType property. But that's not the issue in this post ;)
I'm not blogging about the wellknown song by the Dutch band
Lucifer from the year 1975, but to let you know we (my girlfriend and me) have bought a new house! Somewhere in August of this year, we will be the proud owners of a house in 'Grasrijk', a new part of Eindhoven right next to Veldhoven. After three-somewhat years in our current home we are ready for the next step. And now we can't wait .... !
If you know someone who is interested in an appartment in Eindhoven (look
here for more info),
drop me a line or mail me at rick.van.den.bosch at bloggingabout dot net.
A few days ago we ran into the error in the subject of this post at my current project. Because it wasn't easy finding any usefull hints pointing towards a solution, I'll post ours here.
We use reflection for flexible databinding, based on types and their attributes. One of the methods we wrote sets a value, passed in as an object, on a specific attribute of a specific class. The method knows nothing about the class and its properties. Not even which type the properties are.
We received the error on only 1 attribute, and only when setting its value. It was an sbyte property, which was bound to the SelectedIndex property of a ComboBox. The value passed in the method was a valid value: -1, 0 or 1. But on all possible values, we received the error. Eventually, after reading the QuickWatch in stead of watching it, I saw a small but important difference between the (type of) value I passed in and the (type of) value I wanted to set...
We implemented a GetValue on our controls to return the value, which was used in the method to write it to the object. Because we wanted to bind to the SelectedIndex, the GetValue of the combo we were binding against, looked a bit like this:
object returnValue;
returnValue = SelectedIndex - 1;
return returnValue;
See the problem? The GetValue returned an object, just the way my setter method wanted it. The value of my object was also valid: -1, 0 or 1. But the problem was not the value, neither the returntype of GetValue. The problem is a .Net object 'knows' what type you have put in. So it 'knows' what type it should get out..... (Sure, actually this goes a bit deeper, but for this high-level post, let's leave it at this, OK?)
Because the (integer!) SelectedIndex was put in the returnValue object, the setter method tried to write an integer value to an sbyte property. And that doesn't fit! The implicit cast used is not valid, which results in the above mentioned error. So although the value is correct, the type containing the value isn't, and therefore it cannot be assigned to the sbyte property. Changing the GetValue method to put an sbyte into the object which was returned solved the problem.
I've received lot's of positive reactions on the post about organizing a
bloggingabout.net meeting. I'll try to squeeze in organizing a meeting in the near future. But see the near future somewhere between now and a few months. As you can image lot's of stuff should be taken care of: a place to have the meeting, food & drinks, a decent agenda and maybe even guestspeakers. And to keep everything affordable for all the visitors: maybe we should consider finding a sponsor. But before we go there it's important to make sure it's possible.
So if you feel the need to take up this challenge with me:
drop me a line. I'll start out after next week, because then the deadline on my current project is come and gone.