2 // System.Runtime.Remoting.Messaging.MethodResponse.cs
4 // Author: Duncan Mak (duncan@ximian.com)
7 // 2002 (C) Copyright, Ximian, Inc.
11 using System.Collections;
12 using System.Reflection;
13 using System.Runtime.Remoting;
14 using System.Runtime.Serialization;
16 namespace System.Runtime.Remoting.Messaging {
18 [Serializable] [CLSCompliant (false)]
19 public class MethodResponse : IMethodReturnMessage, ISerializable, IInternalMessage, ISerializationRootObject
24 MethodBase _methodBase;
28 Type [] _methodSignature;
33 IMethodCallMessage _callMsg;
34 LogicalCallContext _callContext;
35 Identity _targetIdentity;
37 protected IDictionary ExternalProperties;
38 protected IDictionary InternalProperties;
40 public MethodResponse (Header[] headers, IMethodCallMessage mcm)
44 _methodName = mcm.MethodName;
46 _typeName = mcm.TypeName;
47 _methodBase = mcm.MethodBase;
48 _methodSignature = (Type[]) mcm.MethodSignature;
54 foreach (Header header in headers)
55 InitMethodProperty (header.Name, header.Value);
59 internal MethodResponse (Exception e, IMethodCallMessage msg) {
69 _outArgs = new object[0]; // .NET does this
72 internal MethodResponse (object returnValue, object [] outArgs, LogicalCallContext callCtx, IMethodCallMessage msg) {
78 _returnValue = returnValue;
82 internal MethodResponse (IMethodCallMessage msg, CADMethodReturnMessage retmsg) {
85 _methodBase = msg.MethodBase;
86 //_typeName = msg.TypeName;
88 _methodName = msg.MethodName;
90 // Get unmarshalled arguments
91 ArrayList args = retmsg.GetArguments ();
93 _exception = retmsg.GetException (args);
94 _returnValue = retmsg.GetReturnValue (args);
95 _outArgs = retmsg.GetArgs (args);
97 _callContext = retmsg.GetLogicalCallContext (args);
98 if (_callContext == null) _callContext = new LogicalCallContext ();
100 if (retmsg.PropertiesCount > 0)
101 CADMessageBase.UnmarshalProperties (Properties, retmsg.PropertiesCount, args);
104 internal MethodResponse (SerializationInfo info, StreamingContext context)
106 foreach (SerializationEntry entry in info)
107 InitMethodProperty (entry.Name, entry.Value);
110 internal void InitMethodProperty (string key, object value)
114 case "__TypeName": _typeName = (string) value; break;
115 case "__MethodName": _methodName = (string) value; break;
116 case "__MethodSignature": _methodSignature = (Type[]) value; break;
117 case "__Uri": _uri = (string) value; break;
118 case "__Return": _returnValue = value; break;
119 case "__OutArgs": _outArgs = (object[]) value; break;
120 case "__fault": _exception = (Exception) value; break;
121 case "__CallContext": _callContext = (LogicalCallContext) value; break;
122 default: Properties [key] = value; break;
126 public int ArgCount {
135 public object[] Args {
141 public Exception Exception {
147 public bool HasVarArgs {
149 return false; // TODO: implement var args
153 public LogicalCallContext LogicalCallContext {
159 public MethodBase MethodBase {
161 if (null == _methodBase && null != _callMsg)
162 _methodBase = _callMsg.MethodBase;
168 public string MethodName {
170 if (null == _methodName && null != _callMsg)
171 _methodName = _callMsg.MethodName;
177 public object MethodSignature {
179 if (null == _methodSignature && null != _callMsg)
180 _methodSignature = (Type []) _callMsg.MethodSignature;
182 return _methodSignature;
186 public int OutArgCount {
188 if (null == _outArgs)
191 return _outArgs.Length;
195 public object[] OutArgs {
197 if (null == _outArgs)
198 return new object[0];
204 public virtual IDictionary Properties {
206 if (null == ExternalProperties) {
207 MethodReturnDictionary properties = new MethodReturnDictionary (this);
208 ExternalProperties = properties;
209 InternalProperties = properties.GetInternalProperties();
212 return ExternalProperties;
216 public object ReturnValue {
222 public string TypeName {
224 if (null == _typeName && null != _callMsg)
225 _typeName = _callMsg.TypeName;
233 if (null == _uri && null != _callMsg)
244 public object GetArg (int argNum)
246 if (null == _outArgs)
249 return _outArgs [argNum];
252 public string GetArgName (int index)
254 throw new NotSupportedException ();
257 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
259 if (_exception == null)
261 info.AddValue ("__TypeName", _typeName);
262 info.AddValue ("__MethodName", _methodName);
263 info.AddValue ("__MethodSignature", _methodSignature);
264 info.AddValue ("__Uri", _uri);
265 info.AddValue ("__Return", _returnValue);
266 info.AddValue ("__OutArgs", _outArgs);
269 info.AddValue ("__fault", _exception);
271 info.AddValue ("__CallContext", _callContext);
273 if (InternalProperties != null) {
274 foreach (DictionaryEntry entry in InternalProperties)
275 info.AddValue ((string) entry.Key, entry.Value);
279 public object GetOutArg (int argNum)
281 if (null == _methodBase)
284 return _outArgs [argNum];
287 public string GetOutArgName (int index)
289 if (null == _methodBase)
290 return "__method_" + index;
292 if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
293 return _inArgInfo.GetInOutArgName(index);
297 public virtual object HeaderHandler (Header[] h)
299 throw new NotImplementedException ();
303 public void RootSetObjectData (SerializationInfo info, StreamingContext context)
305 throw new NotImplementedException ();
308 Identity IInternalMessage.TargetIdentity
310 get { return _targetIdentity; }
311 set { _targetIdentity = value; }
314 public override string ToString ()
316 string s = _typeName.Split(',')[0] + "." + _methodName + " (";
317 if (_exception != null)
319 s += "Exception\n)" + _exception;
323 if (_outArgs != null)
325 for (int n=0; n<_outArgs.Length; n++)
328 if (_outArgs[n] != null) s += _outArgs[n].GetType().Name + " ";
329 s += GetOutArgName (n);
330 if (_outArgs[n] != null) s += " = {" + _outArgs[n] + "}";