· teach ·

introduction

web services make it easy for developers to make their data available to wide ranges of audiences, or access data from a broad array of vendors. through SOAP, data can be wrapped up easily as XML, and given a WSDL descriptor so that the consuming application can learn about the structure of the incoming data. but as always, there are limitations.

i've been doing a lot of .net work lately, and run into a limitation of WDSL when i first attempted to bind a fetched ArrayList of custom objects to a DataGrid in c#. the problem lies in the fact that altho the members are available through a WDSL, the properties do not seem to be, and that is the only way a DataGrid knows how to bind it's data properly.

teh workaround

after trying a variety of options, and searching the web extensively for solutions and even asking people on bulletin boards on asp.net, i finally came upon the solution when i found jan tielen's great article. i'm not going to take credit for the idea of this, or of his original code. the only thing i've done is created a c# version of the class for those of you who don't like to bother with vb, such as myself.

if you want to read about the technical details of the implementation, please visit the original article, for once again i am not taking credit for his work. I did have to change a few things around in the code for it to work in c#. my conversion method takes an IList instead of an object array, since i prefer to use the generic list class in most of my return methods for returning lists of custom objects. i also had to change a few compiler declarations and method names, etc. i'll go into the main problem i ran into next though.

teh problem

so in jan's solution, when he creates a new instance of the dynamically typed object, he can simply call one of that dynamic object's methods, or so it seems, by using:

newObj.SetInnerObject(oldObj)

since c# is a stricter language than vb, this was a complete no go, it couldn't find that dynamically created method during compilation, so of course it bombed. i had to retrieve the MethodInfo for the method i wished to call, create a new object array with the parameter i wished to pass, and Invoke it in the following manner:

System.Reflection.MethodInfo mi =
newType.GetMethod("SetInnerObject");
object[] args = new object[] { oldObj };
mi.Invoke(newObj, System.Reflection.BindingFlags.Default | System.Reflection.BindingFlags.InvokeMethod, null, args, null);


it took a decent while to figure that out, but it seems to be working now.

download source

click to download the zip file here. if you have any problems or questions drop me a message at wall at shawnwall dot com.

resources

© shawn wall 2000-2010 - validate: xhtml css view: rss
shawn\/\/all.com
weblog about images teach links