2011-03-02 37 views
1

我使用C#2.0,如何使用OUT

我想打電話給我的web服務的webmethod在C#從METHOD.INVOKE返回的參數值,請參見下面的代碼示例:

try 
    { 

     Type objtype = Type.GetType(crisresult.ToString()); 
     object obj = Activator.CreateInstance(objtype); 

     Object[] mArgs = new Object[methodArgs.Length + 1]; 
     methodArgs.CopyTo(mArgs, 0); 
     mArgs.SetValue(obj, methodArgs.Length); 
     methodArgs = mArgs; 

     Object result = mi.Invoke(service, methodArgs); 

     JObject parsed = JObject.Parse(result.ToString()); 

     // option 1) if you are just using primitives 
     foreach (KeyValuePair<string, JToken> pair in parsed) 
     { 
      Console.WriteLine("{0}: {1}", pair.Key, pair.Value); 
     }  
    } 

現在在上面的代碼中我的methodArgs對象數組有一個out參數(crisresult),現在從我的webservice方法返回的任何錯誤Object result = mi.Invoke(service, methodArgs);顯示在我的out參數中,我確信我的out參數是我的methodArgs對象中的最後一個對象數組,我正在尋找該對象並檢查從webser返回的錯誤結果副。

請建議!

回答

1

您的methodArgs將被更新爲任何ref/out參數的帳戶。既然你相信這是最後一個:

object foo = methodArgs[methodArgs.Length-1]; 
+0

感謝@馬克,那麼如何閱讀對象爲「foo」的任何財產,說我的名字「消息」的屬性在裏面,如何讀值「message」屬性 – 2011-03-02 12:00:17

+1

在4.0中,'((dynamic)foo).message';在2.0 - 'foo.GetType()。GetProperty(「message」)。GetValue(foo,null)' – 2011-03-02 12:05:51