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