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;
38 using System.Runtime.Serialization.Formatters.Binary;
40 namespace System.Runtime.Remoting.Messaging {
42 [Serializable] [CLSCompliant (false)]
43 [System.Runtime.InteropServices.ComVisible (true)]
44 public class MethodResponse : IMethodReturnMessage, ISerializable, IInternalMessage, ISerializationRootObject
49 MethodBase _methodBase;
53 Type [] _methodSignature;
58 IMethodCallMessage _callMsg;
59 LogicalCallContext _callContext;
60 Identity _targetIdentity;
62 protected IDictionary ExternalProperties;
63 protected IDictionary InternalProperties;
65 public MethodResponse (Header[] h1, IMethodCallMessage mcm)
69 _methodName = mcm.MethodName;
71 _typeName = mcm.TypeName;
72 _methodBase = mcm.MethodBase;
73 _methodSignature = (Type[]) mcm.MethodSignature;
79 foreach (Header header in h1)
80 InitMethodProperty (header.Name, header.Value);
84 internal MethodResponse (Exception e, IMethodCallMessage msg) {
94 _outArgs = new object[0]; // .NET does this
97 internal MethodResponse (object returnValue, object [] outArgs, LogicalCallContext callCtx, IMethodCallMessage msg) {
103 _returnValue = returnValue;
107 internal MethodResponse (IMethodCallMessage msg, CADMethodReturnMessage retmsg) {
110 _methodBase = msg.MethodBase;
111 //_typeName = msg.TypeName;
113 _methodName = msg.MethodName;
115 // Get unmarshalled arguments
116 ArrayList args = retmsg.GetArguments ();
118 _exception = retmsg.GetException (args);
119 _returnValue = retmsg.GetReturnValue (args);
120 _args = retmsg.GetArgs (args);
122 _callContext = retmsg.GetLogicalCallContext (args);
123 if (_callContext == null) _callContext = new LogicalCallContext ();
125 if (retmsg.PropertiesCount > 0)
126 CADMessageBase.UnmarshalProperties (Properties, retmsg.PropertiesCount, args);
129 internal MethodResponse(IMethodCallMessage msg,
130 Object handlerObject,
131 BinaryMethodReturnMessage smuggledMrm)
135 _methodBase = (MethodBase)msg.MethodBase;
136 // _methodCache = InternalRemotingServices.GetReflectionCachedData(MI);
138 _methodName = msg.MethodName;
140 // _typeName = msg.TypeName;
142 // if (_methodCache.IsOverloaded())
143 // _methodSignature = (Type[])msg.MethodSignature;
145 // argCount = _methodCache.Parameters.Length;
149 _returnValue = smuggledMrm.ReturnValue;
150 _args = smuggledMrm.Args;
151 _exception = smuggledMrm.Exception;
153 _callContext = smuggledMrm.LogicalCallContext;
155 if (smuggledMrm.HasProperties)
156 smuggledMrm.PopulateMessageProperties(Properties);
159 internal MethodResponse (SerializationInfo info, StreamingContext context)
161 foreach (SerializationEntry entry in info)
162 InitMethodProperty (entry.Name, entry.Value);
165 internal void InitMethodProperty (string key, object value)
169 case "__TypeName": _typeName = (string) value; break;
170 case "__MethodName": _methodName = (string) value; break;
171 case "__MethodSignature": _methodSignature = (Type[]) value; break;
172 case "__Uri": _uri = (string) value; break;
173 case "__Return": _returnValue = value; break;
174 case "__OutArgs": _args = (object[]) value; break;
175 case "__fault": _exception = (Exception) value; break;
176 case "__CallContext": _callContext = (LogicalCallContext) value; break;
177 default: Properties [key] = value; break;
181 public int ArgCount {
190 public object[] Args {
196 public Exception Exception {
202 public bool HasVarArgs {
204 return (MethodBase.CallingConvention | CallingConventions.VarArgs) != 0;
208 public LogicalCallContext LogicalCallContext {
210 if (_callContext == null)
211 _callContext = new LogicalCallContext ();
216 public MethodBase MethodBase {
218 if (null == _methodBase) {
219 if (_callMsg != null)
220 _methodBase = _callMsg.MethodBase;
221 else if (MethodName != null && TypeName != null)
222 _methodBase = RemotingServices.GetMethodBaseFromMethodMessage (this);
228 public string MethodName {
230 if (null == _methodName && null != _callMsg)
231 _methodName = _callMsg.MethodName;
237 public object MethodSignature {
239 if (null == _methodSignature && null != _callMsg)
240 _methodSignature = (Type []) _callMsg.MethodSignature;
242 return _methodSignature;
246 public int OutArgCount {
248 if (_args == null || _args.Length == 0) return 0;
249 if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
250 return _inArgInfo.GetInOutArgCount ();
254 public object[] OutArgs {
256 if (_outArgs == null && _args != null) {
257 if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
258 _outArgs = _inArgInfo.GetInOutArgs (_args);
264 public virtual IDictionary Properties {
266 if (null == ExternalProperties) {
267 MethodReturnDictionary properties = new MethodReturnDictionary (this);
268 ExternalProperties = properties;
269 InternalProperties = properties.GetInternalProperties();
272 return ExternalProperties;
276 public object ReturnValue {
282 public string TypeName {
284 if (null == _typeName && null != _callMsg)
285 _typeName = _callMsg.TypeName;
293 if (null == _uri && null != _callMsg)
304 string IInternalMessage.Uri {
309 public object GetArg (int argNum)
314 return _args [argNum];
317 public string GetArgName (int index)
319 return MethodBase.GetParameters()[index].Name;
322 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
324 if (_exception == null)
326 info.AddValue ("__TypeName", _typeName);
327 info.AddValue ("__MethodName", _methodName);
328 info.AddValue ("__MethodSignature", _methodSignature);
329 info.AddValue ("__Uri", _uri);
330 info.AddValue ("__Return", _returnValue);
331 info.AddValue ("__OutArgs", _args);
334 info.AddValue ("__fault", _exception);
336 info.AddValue ("__CallContext", _callContext);
338 if (InternalProperties != null) {
339 foreach (DictionaryEntry entry in InternalProperties)
340 info.AddValue ((string) entry.Key, entry.Value);
344 public object GetOutArg (int argNum)
346 if (_args == null) return null;
347 if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
348 return _args[_inArgInfo.GetInOutArgIndex (argNum)];
351 public string GetOutArgName (int index)
353 if (null == _methodBase)
354 return "__method_" + index;
356 if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
357 return _inArgInfo.GetInOutArgName(index);
361 public virtual object HeaderHandler (Header[] h)
363 throw new NotImplementedException ();
367 public void RootSetObjectData (SerializationInfo info, StreamingContext ctx)
369 throw new NotImplementedException ();
372 Identity IInternalMessage.TargetIdentity
374 get { return _targetIdentity; }
375 set { _targetIdentity = value; }
378 bool IInternalMessage.HasProperties()
380 return (ExternalProperties != null) || (InternalProperties != null);