我在我的程序中使用WSProxy类here动态调用Web服务,我需要将返回的对象解析为XML,或者至少访问返回的Web服务结果中的成员。如何在C#中获得动态类型数组的成员?
例如,如果我收到StateCodes的数组,我需要做的:
public object RunService(string webServiceAsmxUrl, string serviceName, string methodName, string jsonArgs)
{
WSDLRuntime.WsProxy wsp = new WSDLRuntime.WsProxy();
// Convert JSON to C# object.
JavaScriptSerializer jser = new JavaScriptSerializer();
var dict = jser.Deserialize<Dictionary<string,object>>(jsonArgs);
// uses mi.Invoke() from the WSProxy class, returns an object.
var result = wsp.CallWebService(webServiceAsmxUrl, serviceName, methodName, dict);
我已经试过各种方法去数组成员,但我打了死胡同。
// THIS WON'T WORK.
// "Cannot apply indexing with [] to an expression of type 'object'"
var firstResult = result[0];
// THIS WON'T WORK.
// "foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'"
foreach (var i in result)
{
}
return object
//At the end of the class, if I try to return the object for XML parsing, I'll get this:
//System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type StateCodes[] may not be used in this context.
//at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType)
因为我不知道这是事先返回数组的类型,我不能做早期绑定。我正在使用C#3.5,我刚刚开始学习。我一直听到“反思”,但我读过的例子似乎并不适用于这个问题。
如果这个问题很混乱,那是因为我很困惑。
就像一个魅力。谢谢! – Jake 2010-11-12 14:21:11