Solving a small bug in the bridges
As I've mentioned yesterday, I was having some problems with bridges. the problem was that every method call returned an error saying that a NullException was thrown. Well, after looking at the generated code from the parsing of the asbx file and doing some debugging, it was clear that the problem was happening in the protected method ConvertToType. The method is inherited from BridgeHandler and looks like this:
protected object ConvertToType(object argValue, Type paramType)
{
string text1 = Assembly.CreateQualifiedName("Microsoft.Web.Extensions", "Microsoft.Web.Script.Serialization.ObjectConverter");
return Type.GetType(text1).GetMethod("ConvertObjectToTypeInternal", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, new object[] { argValue,
paramType,
new JavaScriptSerializer() });
}
The method reuses the private ObjectConverter class introduced by the Extensions dll to perform the conversion. Initialy, I though that the problem was in the ConvertObjectToTypeInternal: i really thought that since the object I was returning back to the server had some null fields, it was causing the problems. In fact, I really think that the ConvertObjectToType method should be called instead since it performs a quick test of seeing if the current object is already of the desired type. Though the ConvertToType object is not virtual, I could "hide" it by using the new keyword and since the method would be defined in the same class that calls it (recall that the method call is performed from the class which results from the parsing of the asbx file"), then by adding that definition in the code behind file I should be able to intercept the call and redirect it to my method. So, I started by adding the following to the bridge class:
protected new object ConvertToType(object argValue, Type paramType)
{
string text1 = Assembly.CreateQualifiedName("Microsoft.Web.Extensions", "Microsoft.Web.Script.Serialization.ObjectConverter");
return Type.GetType(text1).GetMethod("ConvertObjectToType", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, new object[] { argValue,
paramType,
new JavaScriptSerializer() });
}
Well, i was relaly convinced that it would work...but, it didn't! damn...however, now i could debug it and fond out exactly where I was getting the exception. See, the problem is that Type.GetType is returning null! Why is that? Well, I'm not a reflection guru but I think that the problem was that the dll is on the GAC and we need to write its full name. So, changing the previous method to this:
protected new object ConvertToType(object argValue, Type paramType)
{
string text1 = "Microsoft.Web.Script.Serialization.ObjectConverter, Microsoft.Web.Extensions,
Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
return Type.GetType(text1).GetMethod("ConvertObjectToType", BindingFlags.NonPublic |
BindingFlags.Static).Invoke(null, new object[] { argValue, paramType, new JavaScriptSerializer() });
}