* MethodCallMessageWrapper.cs: The wrapper has to modify the wrapped
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Messaging / ReturnMessage.cs
1 //
2 // System.Runtime.Remoting.Messaging.ReturnMessage.cs
3 //
4 // Author:
5 //   Dietmar Maurer (dietmar@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32
33 using System;
34 using System.Collections;
35 using System.Reflection;
36 using System.IO;
37
38 namespace System.Runtime.Remoting.Messaging 
39 {
40         public class ReturnMessage : IMethodReturnMessage, IMethodMessage, IMessage, IInternalMessage 
41         {
42                 object[] _outArgs;
43                 object[] _args;
44                 int _outArgsCount;
45                 LogicalCallContext _callCtx;
46                 object _returnValue;
47                 string _uri;
48                 Exception _exception;
49                 MethodBase _methodBase;
50                 string _methodName;
51                 Type [] _methodSignature;
52                 string _typeName;
53                 MethodReturnDictionary _properties;
54                 Identity _targetIdentity;
55                 ArgInfo _inArgInfo;
56
57                 public ReturnMessage (object returnValue, object [] outArgs,
58                                int outArgCount, LogicalCallContext callCtx,
59                                IMethodCallMessage request)
60                 {
61                         // outArgCount tells how many values of outArgs are valid
62
63                         _returnValue = returnValue;
64                         _args = outArgs;
65                         _outArgsCount = outArgCount;
66                         _callCtx = callCtx;
67                         _uri = request.Uri;
68                         _methodBase = request.MethodBase;
69                         if (_args == null) _args = new object [outArgCount];
70                 }
71
72                 public ReturnMessage (Exception exc, IMethodCallMessage request)
73                 {
74                         _exception = exc;
75                         
76                         if (request != null)
77                                 _methodBase = request.MethodBase;
78                         _args = new object[0];  // .NET does this
79                 }
80                 
81                 public int ArgCount {
82                         get {
83                                 return _args.Length;
84                         }
85                 }
86                 
87                 public object [] Args {
88                         get {
89                                 return _args;
90                         }
91                 }
92                 
93                 public bool HasVarArgs {
94                         get {
95                                 return (MethodBase.CallingConvention | CallingConventions.VarArgs) != 0;
96                         }
97                 }
98
99                 public LogicalCallContext LogicalCallContext {
100                         get {
101                                 return _callCtx;
102                         }
103                 }
104
105                 public MethodBase MethodBase {
106                         get {
107                                 return _methodBase;
108                         }
109                 }
110
111                 public string MethodName {
112                         get {
113                                 if (_methodBase != null && _methodName == null)
114                                         _methodName = _methodBase.Name;
115                                 return _methodName;
116                         }
117                 }
118
119                 public object MethodSignature {
120                         get {
121                                 if (_methodBase != null && _methodSignature == null) {
122                                         ParameterInfo[] parameters = _methodBase.GetParameters();
123                                         _methodSignature = new Type [parameters.Length];
124                                         for (int n=0; n<parameters.Length; n++)
125                                                 _methodSignature[n] = parameters[n].ParameterType;
126                                 }
127                                 return _methodSignature;
128                         }
129                 }
130
131                 public virtual IDictionary Properties {
132                         get {
133                                 if (_properties == null) _properties = new MethodReturnDictionary (this);
134                                 return _properties;
135                         }
136                 }
137
138                 public string TypeName {
139                         get {
140
141                                 // lazily fill in _typeName from _methodBase
142                                 if (_methodBase != null && _typeName == null)
143                                         _typeName = _methodBase.DeclaringType.AssemblyQualifiedName;
144                                 return _typeName;
145
146                         }
147                 }
148
149                 public string Uri {
150                         get {
151                                 return _uri;
152                         }
153
154                         set {
155                                 _uri = value;
156                         }
157                 }
158
159                 public object GetArg (int arg_num)
160                 {
161                         return _args [arg_num];
162                 }
163                 
164                 public string GetArgName (int arg_num)
165                 {
166                         return _methodBase.GetParameters()[arg_num].Name;
167                 }
168
169                 public Exception Exception {
170                         get {
171                                 return _exception;
172                         }
173                 }
174
175                 public int OutArgCount {
176                         get {
177                                 if (_args == null || _args.Length == 0) return 0;
178                                 if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
179                                 return _inArgInfo.GetInOutArgCount ();
180                         }
181                 }
182
183                 public object [] OutArgs {
184                         get {
185                                 if (_outArgs == null && _args != null) {
186                                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
187                                         _outArgs = _inArgInfo.GetInOutArgs (_args);
188                                 }                                       
189                                 return _outArgs;
190                         }
191                 }
192
193                 public virtual object ReturnValue {
194                         get {
195                                 return _returnValue;
196                         }
197                 }
198
199                 public object GetOutArg (int arg_num)
200                 {
201                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
202                         return _args[_inArgInfo.GetInOutArgIndex (arg_num)];
203                 }
204
205                 public string GetOutArgName (int arg_num)
206                 {
207                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
208                         return _inArgInfo.GetInOutArgName(arg_num);
209                 }
210
211                 Identity IInternalMessage.TargetIdentity
212                 {
213                         get { return _targetIdentity; }
214                         set { _targetIdentity = value; }
215                 }
216
217                 public override string ToString ()
218                 {
219                         string s = TypeName.Split(',')[0] + "." + MethodName + " (";
220                         if (_exception != null)
221                         {
222                                 s += "Exception)\n" + _exception;
223                         }
224                         else
225                         {
226                                 for (int n=0; n<OutArgs.Length; n++)
227                                 {
228                                         if (n>0) s+= ", ";
229                                         if (OutArgs[n] != null) s += OutArgs[n].GetType().Name + " ";
230                                         s += GetOutArgName (n);
231                                         if (OutArgs[n] != null) s += " = {" + OutArgs[n] + "}";
232                                         else s+=" = {null}";
233                                 }
234                                 s += ")";
235                         }
236                         return s;
237                 }
238         }
239 }