2 // System.Runtime.Remoting.Messaging.MethodResponse.cs
4 // Author: Duncan Mak (duncan@ximian.com)
7 // 2002 (C) Copyright, Ximian, Inc.
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 using System.Collections;
35 using System.Reflection;
36 using System.Runtime.Remoting;
37 using System.Runtime.Serialization;
39 namespace System.Runtime.Remoting.Messaging {
41 [Serializable] [CLSCompliant (false)]
43 [System.Runtime.InteropServices.ComVisible (true)]
45 public class MethodResponse : IMethodReturnMessage, ISerializable, IInternalMessage, ISerializationRootObject
50 MethodBase _methodBase;
54 Type [] _methodSignature;
59 IMethodCallMessage _callMsg;
60 LogicalCallContext _callContext;
61 Identity _targetIdentity;
63 protected IDictionary ExternalProperties;
64 protected IDictionary InternalProperties;
66 public MethodResponse (Header[] headers, IMethodCallMessage mcm)
70 _methodName = mcm.MethodName;
72 _typeName = mcm.TypeName;
73 _methodBase = mcm.MethodBase;
74 _methodSignature = (Type[]) mcm.MethodSignature;
80 foreach (Header header in headers)
81 InitMethodProperty (header.Name, header.Value);
85 internal MethodResponse (Exception e, IMethodCallMessage msg) {
95 _outArgs = new object[0]; // .NET does this
98 internal MethodResponse (object returnValue, object [] outArgs, LogicalCallContext callCtx, IMethodCallMessage msg) {
104 _returnValue = returnValue;
108 internal MethodResponse (IMethodCallMessage msg, CADMethodReturnMessage retmsg) {
111 _methodBase = msg.MethodBase;
112 //_typeName = msg.TypeName;
114 _methodName = msg.MethodName;
116 // Get unmarshalled arguments
117 ArrayList args = retmsg.GetArguments ();
119 _exception = retmsg.GetException (args);
120 _returnValue = retmsg.GetReturnValue (args);
121 _args = retmsg.GetArgs (args);
123 _callContext = retmsg.GetLogicalCallContext (args);
124 if (_callContext == null) _callContext = new LogicalCallContext ();
126 if (retmsg.PropertiesCount > 0)
127 CADMessageBase.UnmarshalProperties (Properties, retmsg.PropertiesCount, args);
130 internal MethodResponse (SerializationInfo info, StreamingContext context)
132 foreach (SerializationEntry entry in info)
133 InitMethodProperty (entry.Name, entry.Value);
136 internal void InitMethodProperty (string key, object value)
140 case "__TypeName": _typeName = (string) value; break;
141 case "__MethodName": _methodName = (string) value; break;
142 case "__MethodSignature": _methodSignature = (Type[]) value; break;
143 case "__Uri": _uri = (string) value; break;
144 case "__Return": _returnValue = value; break;
145 case "__OutArgs": _args = (object[]) value; break;
146 case "__fault": _exception = (Exception) value; break;
147 case "__CallContext": _callContext = (LogicalCallContext) value; break;
148 default: Properties [key] = value; break;
152 public int ArgCount {
161 public object[] Args {
167 public Exception Exception {
173 public bool HasVarArgs {
175 return (MethodBase.CallingConvention | CallingConventions.VarArgs) != 0;
179 public LogicalCallContext LogicalCallContext {
181 if (_callContext == null)
182 _callContext = new LogicalCallContext ();
187 public MethodBase MethodBase {
189 if (null == _methodBase) {
190 if (_callMsg != null)
191 _methodBase = _callMsg.MethodBase;
192 else if (MethodName != null && TypeName != null)
193 _methodBase = RemotingServices.GetMethodBaseFromMethodMessage (this);
199 public string MethodName {
201 if (null == _methodName && null != _callMsg)
202 _methodName = _callMsg.MethodName;
208 public object MethodSignature {
210 if (null == _methodSignature && null != _callMsg)
211 _methodSignature = (Type []) _callMsg.MethodSignature;
213 return _methodSignature;
217 public int OutArgCount {
219 if (_args == null || _args.Length == 0) return 0;
220 if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
221 return _inArgInfo.GetInOutArgCount ();
225 public object[] OutArgs {
227 if (_outArgs == null && _args != null) {
228 if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
229 _outArgs = _inArgInfo.GetInOutArgs (_args);
235 public virtual IDictionary Properties {
237 if (null == ExternalProperties) {
238 MethodReturnDictionary properties = new MethodReturnDictionary (this);
239 ExternalProperties = properties;
240 InternalProperties = properties.GetInternalProperties();
243 return ExternalProperties;
247 public object ReturnValue {
253 public string TypeName {
255 if (null == _typeName && null != _callMsg)
256 _typeName = _callMsg.TypeName;
264 if (null == _uri && null != _callMsg)
275 string IInternalMessage.Uri {
280 public object GetArg (int argNum)
285 return _args [argNum];
288 public string GetArgName (int index)
290 return MethodBase.GetParameters()[index].Name;
293 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
295 if (_exception == null)
297 info.AddValue ("__TypeName", _typeName);
298 info.AddValue ("__MethodName", _methodName);
299 info.AddValue ("__MethodSignature", _methodSignature);
300 info.AddValue ("__Uri", _uri);
301 info.AddValue ("__Return", _returnValue);
302 info.AddValue ("__OutArgs", _args);
305 info.AddValue ("__fault", _exception);
307 info.AddValue ("__CallContext", _callContext);
309 if (InternalProperties != null) {
310 foreach (DictionaryEntry entry in InternalProperties)
311 info.AddValue ((string) entry.Key, entry.Value);
315 public object GetOutArg (int argNum)
317 if (_args == null) return null;
318 if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
319 return _args[_inArgInfo.GetInOutArgIndex (argNum)];
322 public string GetOutArgName (int index)
324 if (null == _methodBase)
325 return "__method_" + index;
327 if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
328 return _inArgInfo.GetInOutArgName(index);
332 public virtual object HeaderHandler (Header[] h)
334 throw new NotImplementedException ();
338 public void RootSetObjectData (SerializationInfo info, StreamingContext context)
340 throw new NotImplementedException ();
343 Identity IInternalMessage.TargetIdentity
345 get { return _targetIdentity; }
346 set { _targetIdentity = value; }
350 public override string ToString ()
352 string s = _typeName.Split(',')[0] + "." + _methodName + " (";
353 if (_exception != null)
355 s += "Exception\n)" + _exception;
361 for (int n=0; n<_args.Length; n++)
364 if (_args[n] != null) s += _args[n].GetType().Name + " ";
365 s += GetOutArgName (n);
366 if (_args[n] != null) s += " = {" + _args[n] + "}";