Show thumbnail images in SWF.(Open,Save)FileDialog
[mono.git] / mcs / class / corlib / System.Runtime.Remoting.Messaging / MethodCall.cs
1 //
2 // System.Runtime.Remoting.Messaging.MethodCall.cs
3 //
4 // Author: Duncan Mak (duncan@ximian.com)
5 //         Lluis Sanchez Gual (lluis@ideary.com)
6 //
7 // 2002 (C) Copyright, Ximian, Inc.
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.Runtime.Serialization;
37
38 namespace System.Runtime.Remoting.Messaging {
39
40         [Serializable] [CLSCompliant (false)]
41         [System.Runtime.InteropServices.ComVisible (true)]
42         public class MethodCall : IMethodCallMessage, IMethodMessage, IMessage, ISerializable, IInternalMessage, ISerializationRootObject
43         {
44                 string _uri;
45                 string _typeName;
46                 string _methodName;
47                 object[] _args;
48                 Type[] _methodSignature;
49                 MethodBase _methodBase;
50                 LogicalCallContext _callContext;
51                 ArgInfo _inArgInfo;
52                 Identity _targetIdentity;
53                 Type[] _genericArguments;
54
55                 protected IDictionary ExternalProperties;
56                 protected IDictionary InternalProperties;
57
58                 public MethodCall (Header [] h1)
59                 {
60                         Init();
61
62                         if (h1 == null || h1.Length == 0) return;
63
64                         foreach (Header header in h1)
65                                 InitMethodProperty (header.Name, header.Value);
66
67                         ResolveMethod ();
68                 }
69
70                 internal MethodCall (SerializationInfo info, StreamingContext context)
71                 {
72                         Init();
73
74                         foreach (SerializationEntry entry in info)
75                                 InitMethodProperty ((string)entry.Name, entry.Value);
76                 }
77
78                 internal MethodCall (CADMethodCallMessage msg) 
79                 {
80                         _uri = string.Copy (msg.Uri);
81                         
82                         // Get unmarshalled arguments
83                         ArrayList args = msg.GetArguments ();
84
85                         _args = msg.GetArgs (args);
86                         _callContext = msg.GetLogicalCallContext (args);
87                         if (_callContext == null)
88                                 _callContext = new LogicalCallContext ();
89         
90                         _methodBase = msg.GetMethod ();
91                         
92                         Init();
93
94                         if (msg.PropertiesCount > 0)
95                                 CADMessageBase.UnmarshalProperties (Properties, msg.PropertiesCount, args);
96                 }
97
98                 public MethodCall (IMessage msg)
99                 {
100                         if (msg is IMethodMessage)
101                                 CopyFrom ((IMethodMessage) msg);
102                         else
103                         {
104                                 foreach (DictionaryEntry entry in msg.Properties)
105                                         InitMethodProperty ((String) entry.Key, entry.Value);
106                                 Init();
107                 }
108                 }
109
110                 internal MethodCall (string uri, string typeName, string methodName, object[] args)
111                 {
112                         _uri = uri;
113                         _typeName = typeName;
114                         _methodName = methodName;
115                         _args = args;
116
117                         Init();
118                         ResolveMethod();
119                 }
120
121                 internal MethodCall ()
122                 {
123                 }
124                 
125                 internal void CopyFrom (IMethodMessage call)
126                 {
127                         _uri = call.Uri;
128                         _typeName = call.TypeName;
129                         _methodName = call.MethodName;
130                         _args = call.Args;
131                         _methodSignature = (Type[]) call.MethodSignature;
132                         _methodBase = call.MethodBase;
133                         _callContext = call.LogicalCallContext;
134                         Init();
135                 }
136                 
137                 internal virtual void InitMethodProperty(string key, object value)
138                 {
139                         switch (key)
140                         {
141                                 case "__TypeName" : _typeName = (string) value; return;
142                                 case "__MethodName" : _methodName = (string) value; return;
143                                 case "__MethodSignature" : _methodSignature = (Type[]) value; return;
144                                 case "__Args" : _args = (object[]) value; return;
145                                 case "__CallContext" : _callContext = (LogicalCallContext) value; return;
146                                 case "__Uri" : _uri = (string) value; return;
147                                 case "__GenericArguments" : _genericArguments = (Type[]) value; return;
148                                 default: Properties[key] = value; return;
149                         }
150                 }
151
152                 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
153                 {
154                         info.AddValue ("__TypeName", _typeName);
155                         info.AddValue ("__MethodName", _methodName);
156                         info.AddValue ("__MethodSignature", _methodSignature);
157                         info.AddValue ("__Args", _args);
158                         info.AddValue ("__CallContext", _callContext);
159                         info.AddValue ("__Uri", _uri);
160                         info.AddValue ("__GenericArguments", _genericArguments);
161
162                         if (InternalProperties != null) {
163                                 foreach (DictionaryEntry entry in InternalProperties)
164                                         info.AddValue ((string) entry.Key, entry.Value);
165                         }
166                 } 
167
168                 public int ArgCount {
169                         get { return _args.Length; }
170                 }
171
172                 public object[] Args {
173                         get { return _args; }
174                 }
175                 
176                 public bool HasVarArgs {
177                         get { return (MethodBase.CallingConvention | CallingConventions.VarArgs) != 0; }
178                 }
179
180                 public int InArgCount 
181                 {
182                         get 
183                         { 
184                                 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
185                                 return _inArgInfo.GetInOutArgCount();
186                         }
187                 }
188
189                 public object[] InArgs 
190                 {
191                         get 
192                         { 
193                                 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
194                                 return _inArgInfo.GetInOutArgs (_args);
195                         }
196                 }
197                 
198                 public LogicalCallContext LogicalCallContext {
199                         get {
200                                 if (_callContext == null)
201                                         _callContext = new LogicalCallContext ();
202                                 return _callContext;
203                         }
204                 }
205                 
206                 public MethodBase MethodBase {
207                         get {
208                                 if (_methodBase == null)
209                                         ResolveMethod ();
210                                         
211                                 return _methodBase;
212                         }
213                 }
214
215                 public string MethodName {
216                         get {
217                                 // lazily fill in _methodName from _methodBase
218                                 if (_methodName == null)
219                                         _methodName = _methodBase.Name;
220                                 return _methodName;
221                         }
222                 }
223
224                 public object MethodSignature {
225                         get { 
226                                 if (_methodSignature == null && _methodBase != null)
227                                 {
228                                         ParameterInfo[] parameters = _methodBase.GetParameters();
229                                         _methodSignature = new Type[parameters.Length];
230                                         for (int n=0; n<parameters.Length; n++)
231                                                 _methodSignature[n] = parameters[n].ParameterType;
232                                 }
233                                 return _methodSignature;
234                         }
235                 }
236
237                 public virtual IDictionary Properties {
238                         get 
239                         { 
240                                 if (ExternalProperties == null) InitDictionary ();
241                                 return ExternalProperties; 
242                         }
243                 }
244
245                 internal virtual void InitDictionary()
246                 {
247                         MethodCallDictionary props = new MethodCallDictionary (this);
248                         ExternalProperties = props;
249                         InternalProperties = props.GetInternalProperties();
250                 }
251
252                 public string TypeName 
253                 {
254                         get {
255                                 // lazily fill in _typeName from _methodBase
256                                 if (_typeName == null)
257                                         _typeName = _methodBase.DeclaringType.AssemblyQualifiedName;
258                                 return _typeName;
259                         }
260                 }
261
262                 public string Uri {
263                         get { return _uri; }
264                         set { _uri = value; }
265                 }
266
267                 string IInternalMessage.Uri {
268                         get { return Uri; }
269                         set { Uri = value; }
270                 }
271
272                 public object GetArg (int argNum)
273                 {
274                         return _args[argNum];
275                 }
276
277                 public string GetArgName (int index)
278                 {
279                         return _methodBase.GetParameters()[index].Name;
280                 }
281
282                 public object GetInArg (int argNum)
283                 {
284                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
285                         return _args[_inArgInfo.GetInOutArgIndex (argNum)];
286                 }
287
288                 public string GetInArgName (int index)
289                 {
290                         if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
291                         return _inArgInfo.GetInOutArgName(index);
292                 }
293
294                 [MonoTODO]
295                 public virtual object HeaderHandler (Header[] h)
296                 {
297                         throw new NotImplementedException ();
298                 }
299
300                 public virtual void Init ()
301                 {
302                 }
303
304                 public void ResolveMethod ()
305                 {
306                         if (_uri != null)
307                         {
308                                 Type type = RemotingServices.GetServerTypeForUri (_uri);
309                                 if (type == null) {
310                                         string sname = _typeName != null ? " (" + _typeName + ")" : "";
311                                         throw new RemotingException ("Requested service not found" + sname + ". No receiver for uri " + _uri);
312                                 }
313
314                                 Type requestType = CastTo (_typeName, type);
315                                 if (requestType == null)
316                                         throw new RemotingException ("Cannot cast from client type '" + _typeName + "' to server type '" + type.FullName + "'");
317
318                                 // Look for the method in the requested type. The method signature is provided
319                                 // only if the method is overloaded in the requested type.
320                                 _methodBase = RemotingServices.GetMethodBaseFromName (requestType, _methodName, _methodSignature);
321
322                                 if (_methodBase == null)
323                                         throw new RemotingException ("Method " + _methodName + " not found in " + requestType);
324
325                                 // If the method is implemented in an interface, look for the method implementation.
326                                 // It can't be done in the previous GetMethodBaseFromName call because at that point we
327                                 // may not yet have the method signature.
328                                 if (requestType != type && requestType.IsInterface && !type.IsInterface) {
329                                         _methodBase = RemotingServices.GetVirtualMethod (type, _methodBase);
330                                         if (_methodBase == null)
331                                                 throw new RemotingException ("Method " + _methodName + " not found in " + type);
332                                 }
333
334                         } else {
335                                 _methodBase = RemotingServices.GetMethodBaseFromMethodMessage (this);
336                                 if (_methodBase == null) throw new RemotingException ("Method " + _methodName + " not found in " + TypeName);
337                         }
338
339
340                         if (_methodBase.IsGenericMethod && _methodBase.ContainsGenericParameters) {
341                                 if (GenericArguments == null)
342                                         throw new RemotingException ("The remoting infrastructure does not support open generic methods.");
343                                 _methodBase = ((MethodInfo) _methodBase).MakeGenericMethod (GenericArguments);
344                         }
345                 }
346
347                 Type CastTo (string clientType, Type serverType)
348                 {
349                         clientType = GetTypeNameFromAssemblyQualifiedName (clientType);
350                         if (clientType == serverType.FullName) return serverType;
351
352                         // base class hierarchy
353
354                         Type baseType = serverType.BaseType;
355                         while (baseType != null) {
356                                 if (clientType == baseType.FullName) return baseType;
357                                 baseType = baseType.BaseType;
358                         }
359
360                         // Implemented interfaces
361
362                         Type[] interfaces = serverType.GetInterfaces();
363                         foreach (Type itype in interfaces)
364                                 if (clientType == itype.FullName) return itype;
365      
366                         return null;
367                 }
368
369                 static string GetTypeNameFromAssemblyQualifiedName (string aqname)
370                 {
371                         int p = aqname.IndexOf ("]]");
372                         int i = aqname.IndexOf(',', p == -1 ? 0 : p + 2);
373                         if (i != -1) aqname = aqname.Substring (0, i).Trim ();
374                         return aqname;
375                 }
376                 
377                 [MonoTODO]
378                 public void RootSetObjectData (SerializationInfo info, StreamingContext ctx)
379                 {
380                         throw new NotImplementedException ();
381                 }
382
383                 Identity IInternalMessage.TargetIdentity
384                 {
385                         get { return _targetIdentity; }
386                         set { _targetIdentity = value; }
387                 }
388
389                 Type[] GenericArguments {
390                         get {
391                                 if (_genericArguments != null)
392                                         return _genericArguments;
393
394                                 return _genericArguments = MethodBase.GetGenericArguments ();
395                         }
396                 }
397         }
398 }