2 // System.Runtime.Remoting.Messaging.MethodCall.cs
4 // Author: Duncan Mak (duncan@ximian.com)
5 // Lluis Sanchez Gual (lluis@ideary.com)
7 // 2002 (C) Copyright, Ximian, Inc.
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
34 using System.Collections;
35 using System.Reflection;
36 using System.Runtime.Serialization;
37 using System.Runtime.Serialization.Formatters.Binary;
39 namespace System.Runtime.Remoting.Messaging {
41 [Serializable] [CLSCompliant (false)]
42 [System.Runtime.InteropServices.ComVisible (true)]
43 public class MethodCall : IMethodCallMessage, IMethodMessage, IMessage, ISerializable, IInternalMessage, ISerializationRootObject
49 Type[] _methodSignature;
50 MethodBase _methodBase;
51 LogicalCallContext _callContext;
53 Identity _targetIdentity;
54 Type[] _genericArguments;
56 protected IDictionary ExternalProperties;
57 protected IDictionary InternalProperties;
59 public MethodCall (Header [] h1)
63 if (h1 == null || h1.Length == 0) return;
65 foreach (Header header in h1)
66 InitMethodProperty (header.Name, header.Value);
71 internal MethodCall (SerializationInfo info, StreamingContext context)
75 foreach (SerializationEntry entry in info)
76 InitMethodProperty ((string)entry.Name, entry.Value);
79 internal MethodCall (CADMethodCallMessage msg)
81 _uri = string.Copy (msg.Uri);
83 // Get unmarshalled arguments
84 ArrayList args = msg.GetArguments ();
86 _args = msg.GetArgs (args);
87 _callContext = msg.GetLogicalCallContext (args);
88 if (_callContext == null)
89 _callContext = new LogicalCallContext ();
91 _methodBase = msg.GetMethod ();
95 if (msg.PropertiesCount > 0)
96 CADMessageBase.UnmarshalProperties (Properties, msg.PropertiesCount, args);
99 public MethodCall (IMessage msg)
101 if (msg is IMethodMessage)
102 CopyFrom ((IMethodMessage) msg);
105 foreach (DictionaryEntry entry in msg.Properties)
106 InitMethodProperty ((String) entry.Key, entry.Value);
111 internal MethodCall (string uri, string typeName, string methodName, object[] args)
114 _typeName = typeName;
115 _methodName = methodName;
122 internal MethodCall (Object handlerObject, BinaryMethodCallMessage smuggledMsg)
124 if (handlerObject != null)
126 _uri = handlerObject as String;
129 // This must be the tranparent proxy
130 MarshalByRefObject mbr = handlerObject as MarshalByRefObject;
133 throw new NotImplementedException ("MarshalByRefObject.GetIdentity");
136 srvID = MarshalByRefObject.GetIdentity(mbr, out fServer) as ServerIdentity;
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;
152 if (smuggledMsg.HasProperties)
153 smuggledMsg.PopulateMessageProperties(Properties);
156 internal MethodCall ()
160 internal void CopyFrom (IMethodMessage call)
163 _typeName = call.TypeName;
164 _methodName = call.MethodName;
166 _methodSignature = (Type[]) call.MethodSignature;
167 _methodBase = call.MethodBase;
168 _callContext = call.LogicalCallContext;
172 internal virtual void InitMethodProperty(string key, object value)
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;
187 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
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);
197 if (InternalProperties != null) {
198 foreach (DictionaryEntry entry in InternalProperties)
199 info.AddValue ((string) entry.Key, entry.Value);
203 public int ArgCount {
204 get { return _args.Length; }
207 public object[] Args {
208 get { return _args; }
211 public bool HasVarArgs {
212 get { return (MethodBase.CallingConvention | CallingConventions.VarArgs) != 0; }
215 public int InArgCount
219 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
220 return _inArgInfo.GetInOutArgCount();
224 public object[] InArgs
228 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
229 return _inArgInfo.GetInOutArgs (_args);
233 public LogicalCallContext LogicalCallContext {
235 if (_callContext == null)
236 _callContext = new LogicalCallContext ();
241 public MethodBase MethodBase {
243 if (_methodBase == null)
250 public string MethodName {
252 // lazily fill in _methodName from _methodBase
253 if (_methodName == null)
254 _methodName = _methodBase.Name;
259 public object MethodSignature {
261 if (_methodSignature == null && _methodBase != null)
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;
268 return _methodSignature;
272 public virtual IDictionary Properties {
275 if (ExternalProperties == null) InitDictionary ();
276 return ExternalProperties;
280 internal virtual void InitDictionary()
282 MethodCallDictionary props = new MethodCallDictionary (this);
283 ExternalProperties = props;
284 InternalProperties = props.GetInternalProperties();
287 public string TypeName
290 // lazily fill in _typeName from _methodBase
291 if (_typeName == null)
292 _typeName = _methodBase.DeclaringType.AssemblyQualifiedName;
299 set { _uri = value; }
302 string IInternalMessage.Uri {
307 public object GetArg (int argNum)
309 return _args[argNum];
312 public string GetArgName (int index)
314 return _methodBase.GetParameters()[index].Name;
317 public object GetInArg (int argNum)
319 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
320 return _args[_inArgInfo.GetInOutArgIndex (argNum)];
323 public string GetInArgName (int index)
325 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
326 return _inArgInfo.GetInOutArgName(index);
330 public virtual object HeaderHandler (Header[] h)
332 throw new NotImplementedException ();
335 public virtual void Init ()
339 public void ResolveMethod ()
343 Type type = RemotingServices.GetServerTypeForUri (_uri);
345 string sname = _typeName != null ? " (" + _typeName + ")" : "";
346 throw new RemotingException ("Requested service not found" + sname + ". No receiver for uri " + _uri);
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 + "'");
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);
357 if (_methodBase == null)
358 throw new RemotingException ("Method " + _methodName + " not found in " + requestType);
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);
370 _methodBase = RemotingServices.GetMethodBaseFromMethodMessage (this);
371 if (_methodBase == null) throw new RemotingException ("Method " + _methodName + " not found in " + TypeName);
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);
382 Type CastTo (string clientType, Type serverType)
384 clientType = GetTypeNameFromAssemblyQualifiedName (clientType);
385 if (clientType == serverType.FullName) return serverType;
387 // base class hierarchy
389 Type baseType = serverType.BaseType;
390 while (baseType != null) {
391 if (clientType == baseType.FullName) return baseType;
392 baseType = baseType.BaseType;
395 // Implemented interfaces
397 Type[] interfaces = serverType.GetInterfaces();
398 foreach (Type itype in interfaces)
399 if (clientType == itype.FullName) return itype;
404 static string GetTypeNameFromAssemblyQualifiedName (string aqname)
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 ();
413 public void RootSetObjectData (SerializationInfo info, StreamingContext ctx)
415 throw new NotImplementedException ();
418 Identity IInternalMessage.TargetIdentity
420 get { return _targetIdentity; }
421 set { _targetIdentity = value; }
424 bool IInternalMessage.HasProperties()
426 return (ExternalProperties != null) || (InternalProperties != null);
429 Type[] GenericArguments {
431 if (_genericArguments != null)
432 return _genericArguments;
434 return _genericArguments = MethodBase.GetGenericArguments ();