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