我在我的程序中使用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