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