aee39f5bb81103129491a363a4e1641fd44a3d56
[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 #if NET_2_0
41         [System.Runtime.InteropServices.ComVisible (true)]
42 #endif
43         public class ReturnMessage : IMethodReturnMessage, IMethodMessage, IMessage, IInternalMessage 
44         {
45                 object[] _outArgs;
46                 object[] _args;
47                 int _outArgsCount;
48                 LogicalCallContext _callCtx;
49                 object _returnValue;
50                 string _uri;
51                 Exception _exception;
52                 MethodBase _methodBase;
53                 string _methodName;
54                 Type [] _methodSignature;
55                 string _typeName;
56                 MethodReturnDictionary _properties;
57                 Identity _targetIdentity;
58                 ArgInfo _inArgInfo;
59
60                 public ReturnMessage (object returnValue, object [] outArgs,
61                                int outArgCount, LogicalCallContext callCtx,
62                                IMethodCallMessage request)
63                 {
64                         // outArgCount tells how many values of outArgs are valid
65
66                         _returnValue = returnValue;
67                         _args = outArgs;
68                         _outArgsCount = outArgCount;
69                         _callCtx = callCtx;
70                         if (request != null) {
71                                 _uri = request.Uri;
72                                 _methodBase = request.MethodBase;
73                         }
74                         if (_args == null) _args = new object [outArgCount];
75                 }
76
77                 public ReturnMessage (Exception exc, IMethodCallMessage request)
78                 {
79                         _exception = exc;
80                         
81                         if (request != null) {
82                                 _methodBase = request.MethodBase;
83                                 _callCtx = request.LogicalCallContext;
84                         }
85                         _args = new object[0];  // .NET does this
86                 }
87                 
88                 public int ArgCount {
89                         get {
90                                 return _args.Length;
91                         }
92                 }
93                 
94                 public object [] Args {
95                         get {
96                                 return _args;
97                         }
98                 }
99                 
100                 public bool HasVarArgs {
101                         get {
102                                 if (_methodBase == null) return false;
103                                 else return (_methodBase.CallingConvention | CallingConventions.VarArgs) != 0;
104                         }
105                 }
106
107                 public LogicalCallContext LogicalCallContext {
108                         get {
109                                 if (_callCtx == null)
110                                         _callCtx = new LogicalCallContext ();
111                                 return _callCtx;
112                         }
113                 }
114
115                 public MethodBase MethodBase {
116                         get {
117                                 return _methodBase;
118                         }
119                 }
120
121                 public string MethodName {
122                         get {
123                                 if (_methodBase != null && _methodName == null)
124                                         _methodName = _methodBase.Name;
125                                 return _methodName;
126                         }
127                 }
128
129                 public object MethodSignature {
130                         get {
131                                 if (_methodBase != null && _methodSignature == null) {
132                                         ParameterInfo[] parameters = _methodBase.GetParameters();
133                                         _methodSignature = new Type [parameters.Length];
134                                         for (int n=0; n<parameters.Length; n++)
135                                                 _methodSignature[n] = parameters[n].ParameterType;
136                                 }
137                                 return _methodSignature;
138                         }
139                 }
140
141                 public virtual IDictionary Properties {
142                         get {
143                                 if (_properties == null) _properties = new MethodReturnDictionary (this);
144                                 return _properties;
145                         }
146                 }
147
148                 public string TypeName {
149                         get {
150
151                                 // lazily fill in _typeName from _methodBase
152                                 if (_methodBase != null && _typeName == null)
153                                         _typeName = _methodBase.DeclaringType.AssemblyQualifiedName;
154                                 return _typeName;
155
156                         }
157                 }
158
159                 public string Uri {
160                         get {
161                                 return _uri;
162                         }
163
164                         set {
165                                 _uri = value;
166                         }
167                 }
168
169                 string IInternalMessage.Uri {
170                         get { return Uri; }
171                         set { Uri = value; }
172                 }
173
174                 public object GetArg (int arg_num)
175                 {
176                         return _args [arg_num];
177                 }
178                 
179                 public string GetArgName (int arg_num)
180                 {
181                         return _methodBase.GetParameters()[arg_num].Name;
182                 }
183
184                 public Exception Exception {
185                         get {
186                                 return _exception;
187                         }
188                 }
189
190                 public int OutArgCount {
191                         get {
192                                 if (_args == null || _args.Length == 0) return 0;
193                                 if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
194                                 return _inArgInfo.GetInOutArgCount ();
195                         }
196                 }
197
198                 public object [] OutArgs {
199                         get {
200                                 if (_outArgs == null && _args != null) {
201                                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
202                                         _outArgs = _inArgInfo.GetInOutArgs (_args);
203                                 }                                       
204                                 return _outArgs;
205                         }
206                 }
207
208                 public virtual object ReturnValue {
209                         get {
210                                 return _returnValue;
211                         }
212                 }
213
214                 public object GetOutArg (int arg_num)
215                 {
216                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
217                         return _args[_inArgInfo.GetInOutArgIndex (arg_num)];
218                 }
219
220                 public string GetOutArgName (int arg_num)
221                 {
222                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (MethodBase, ArgInfoType.Out);
223                         return _inArgInfo.GetInOutArgName(arg_num);
224                 }
225
226                 Identity IInternalMessage.TargetIdentity
227                 {
228                         get { return _targetIdentity; }
229                         set { _targetIdentity = value; }
230                 }
231
232 #if !NET_2_0
233                 public override string ToString ()
234                 {
235                         string s = TypeName.Split(',')[0] + "." + MethodName + " (";
236                         if (_exception != null)
237                         {
238                                 s += "Exception)\n" + _exception;
239                         }
240                         else
241                         {
242                                 for (int n=0; n<OutArgs.Length; n++)
243                                 {
244                                         if (n>0) s+= ", ";
245                                         if (OutArgs[n] != null) s += OutArgs[n].GetType().Name + " ";
246                                         s += GetOutArgName (n);
247                                         if (OutArgs[n] != null) s += " = {" + OutArgs[n] + "}";
248                                         else s+=" = {null}";
249                                 }
250                                 s += ")";
251                         }
252                         return s;
253                 }
254 #endif
255         }
256 }