Wednesday, April 21, 2010

Passing object[] from a webmethod

A PageMethod returning a object[] will be the ugliest of solutions, you would say. Sometimes, you are forced to code in a not-so-elegant style. And here's one such implementation. If any of you think you can make it better, please leave a comment.

Another solution to this will be to wrap all these objects up into a class and have this as the return type.

public class MyReturnType {
public ClassA A { get;set; }
public ClassB B { get;set; }
public ClassC C { get;set; }

and return an instance of MyReturnType (or List for multiple values)

Returning this composite MyReturnType object will be the ideal solution. But in my case, the elements of this class are known only at run time. Typically, this class will need to be build at run time. The easier alternate will be to

public object[] GetObjects()
return new object[] { objA, objB, objC };