* AsyncResult.cs: implemented some methods.
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Messaging / MonoMethodMessage.cs
1 //
2 // System.Runtime.Remoting.Messaging.MonoMethodMessage.cs
3 //
4 // Author:
5 //   Dietmar Maurer (dietmar@ximian.com)
6 //   Patrik Torstensson
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 //
10
11 using System;
12 using System.Collections;
13 using System.Reflection;
14 using System.Runtime.CompilerServices;
15
16 namespace System.Runtime.Remoting.Messaging {
17         
18         [Serializable]
19         public class MonoMethodMessage : IMethodCallMessage, IMethodReturnMessage, IInternalMessage {
20
21                 MonoMethod method;
22
23                 object []  args;
24
25                 string []  names;
26
27                 byte [] arg_types; /* 1 == IN; 2 == OUT ; 3 = INOUT */
28
29                 public LogicalCallContext ctx;
30
31                 public object rval;
32
33                 public Exception exc;
34
35                 AsyncResult asyncResult;
36
37                 string uri;
38
39                 MethodCallDictionary properties;
40
41                 Type[] methodSignature;
42
43                 Identity identity;
44
45
46                 [MethodImplAttribute(MethodImplOptions.InternalCall)]
47                 internal extern void InitMessage (MonoMethod method, object [] out_args);
48
49                 public MonoMethodMessage (MethodBase method, object [] out_args)
50                 {
51                         if (method != null)
52                                 InitMessage ((MonoMethod)method, out_args);
53                         else
54                                 args = null;
55                 }
56
57                 public MonoMethodMessage (Type type, string method_name, object [] in_args)
58                 {
59                         // fixme: consider arg types
60                         MethodInfo minfo = type.GetMethod (method_name);
61                         
62                         InitMessage ((MonoMethod)minfo, null);
63
64                         int len = in_args.Length;
65                         for (int i = 0; i < len; i++) {
66                                 args [i] = in_args [i];
67                         }
68                 }
69                 
70                 public IDictionary Properties {
71                         get {
72                                 if (properties == null) properties = new MethodCallDictionary (this);
73                                 return properties;
74                         }
75                 }
76
77                 public int ArgCount {
78                         get {
79                                 if (null == args)
80                                         return 0;
81
82                                 return args.Length;
83                         }
84                 }
85                 
86                 public object [] Args {
87                         get {
88                                 return args;
89                         }
90                 }
91                 
92                 public bool HasVarArgs {
93                         get {
94                                 return false;
95                         }
96                 }
97
98                 public LogicalCallContext LogicalCallContext {
99                         get {
100                                 return ctx;
101                         }
102                 }
103
104                 public MethodBase MethodBase {
105                         get {
106                                 return method;
107                         }
108                 }
109
110                 public string MethodName {
111                         get {
112                                 if (null == method)
113                                         return String.Empty;
114
115                                 return method.Name;
116                         }
117                 }
118
119                 public object MethodSignature {
120                         get {
121                                 if (methodSignature == null) {
122                                         ParameterInfo[] parameters = method.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 string TypeName {
132                         get {
133                                 if (null == method)
134                                         return String.Empty;
135
136                                 return method.DeclaringType.AssemblyQualifiedName;
137                         }
138                 }
139
140                 public string Uri {
141                         get {
142                                 return uri;
143                         }
144
145                         set {
146                                 uri = value;
147                         }
148                 }
149
150                 public object GetArg (int arg_num)
151                 {
152                         if (null == args)
153                                 return null;
154
155                         return args [arg_num];
156                 }
157                 
158                 public string GetArgName (int arg_num)
159                 {
160                         if (null == args)
161                                 return String.Empty;
162
163                         return names [arg_num];
164                 }
165
166                 public int InArgCount {
167                         get {
168                                 if (null == args)
169                                         return 0;
170
171                                 int count = 0;
172
173                                 foreach (byte t in arg_types) {
174                                         if ((t & 1) != 0) count++;
175                                                 
176                                 }
177                                 return count;
178                         }
179                 }
180                 
181                 public object [] InArgs {
182                         get {                
183                                 int i, j, count = InArgCount;
184                                 object [] inargs = new object [count];
185
186                                 i = j = 0;
187                                 foreach (byte t in arg_types) {
188                                         if ((t & 1) != 0)
189                                                 inargs [j++] = args [i];
190                                         i++;
191                                 }
192                                 
193                                 return inargs;
194                         }
195                 }
196                 
197                 public object GetInArg (int arg_num)
198                 {
199                         int i = 0, j = 0;
200                         foreach (byte t in arg_types) {
201                                 if ((t & 1) != 0) {
202                                         if (j++ == arg_num)
203                                                 return args [i]; 
204                                 }
205                                 i++;
206                         }
207                         return null;
208                 }
209                 
210                 public string GetInArgName (int arg_num)
211                 {
212                         int i = 0, j = 0;
213                         foreach (byte t in arg_types) {
214                                 if ((t & 1) != 0) {
215                                         if (j++ == arg_num)
216                                                 return names [i]; 
217                                 }
218                                 i++;
219                         }
220                         return null;
221                 }
222
223                 public Exception Exception {
224                         get {
225                                 return exc;
226                         }
227                 }
228                 
229                 public int OutArgCount {
230                         get {
231                                 if (null == args)
232                                         return 0;
233                                 
234                                 int count = 0;
235
236                                 foreach (byte t in arg_types) {
237                                         if ((t & 2) != 0) count++;
238                                                 
239                                 }
240                                 return count;
241                         }
242                 }
243                 
244                 public object [] OutArgs {
245                         get {
246                                 if (null == args)
247                                         return null;
248
249                                 int i, j, count = OutArgCount;
250                                 object [] outargs = new object [count];
251
252                                 i = j = 0;
253                                 foreach (byte t in arg_types) {
254                                         if ((t & 2) != 0)
255                                                 outargs [j++] = args [i];
256                                         i++;
257                                 }
258                                 
259                                 return outargs;
260                         }
261                 }
262                 
263                 public object ReturnValue {
264                         get {
265                                 return rval;
266                         }
267                 }
268
269                 public object GetOutArg (int arg_num)
270                 {
271                         int i = 0, j = 0;
272                         foreach (byte t in arg_types) {
273                                 if ((t & 2) != 0) {
274                                         if (j++ == arg_num)
275                                                 return args [i]; 
276                                 }
277                                 i++;
278                         }
279                         return null;
280                 }
281                 
282                 public string GetOutArgName (int arg_num)
283                 {
284                         int i = 0, j = 0;
285                         foreach (byte t in arg_types) {
286                                 if ((t & 2) != 0) {
287                                         if (j++ == arg_num)
288                                                 return names [i]; 
289                                 }
290                                 i++;
291                         }
292                         return null;
293                 }
294
295                 Identity IInternalMessage.TargetIdentity
296                 {
297                         get { return identity; }
298                         set { identity = value; }
299                 }
300
301                 public bool IsAsync
302                 {
303                         get { return asyncResult != null; }
304                 }
305
306                 public AsyncResult AsyncResult
307                 {
308                         get { return asyncResult; }
309                 }
310         }
311 }