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;
38 namespace System.Runtime.Remoting.Messaging {
40 [Serializable] [CLSCompliant (false)]
42 [System.Runtime.InteropServices.ComVisible (true)]
44 public class MethodCall : IMethodCallMessage, IMethodMessage, IMessage, ISerializable, IInternalMessage, ISerializationRootObject
50 Type[] _methodSignature;
51 MethodBase _methodBase;
52 LogicalCallContext _callContext;
54 Identity _targetIdentity;
56 Type[] _genericArguments;
59 protected IDictionary ExternalProperties;
60 protected IDictionary InternalProperties;
62 public MethodCall (Header [] headers)
66 if (headers == null || headers.Length == 0) return;
68 foreach (Header header in headers)
69 InitMethodProperty (header.Name, header.Value);
74 internal MethodCall (SerializationInfo info, StreamingContext context)
78 foreach (SerializationEntry entry in info)
79 InitMethodProperty ((string)entry.Name, entry.Value);
82 internal MethodCall (CADMethodCallMessage msg)
84 _uri = string.Copy (msg.Uri);
86 // Get unmarshalled arguments
87 ArrayList args = msg.GetArguments ();
89 _args = msg.GetArgs (args);
90 _callContext = msg.GetLogicalCallContext (args);
91 if (_callContext == null)
92 _callContext = new LogicalCallContext ();
94 _methodBase = msg.GetMethod ();
98 if (msg.PropertiesCount > 0)
99 CADMessageBase.UnmarshalProperties (Properties, msg.PropertiesCount, args);
102 public MethodCall (IMessage msg)
104 if (msg is IMethodMessage)
105 CopyFrom ((IMethodMessage) msg);
108 foreach (DictionaryEntry entry in msg.Properties)
109 InitMethodProperty ((String) entry.Key, entry.Value);
114 internal MethodCall (string uri, string typeName, string methodName, object[] args)
117 _typeName = typeName;
118 _methodName = methodName;
125 internal MethodCall ()
129 internal void CopyFrom (IMethodMessage call)
132 _typeName = call.TypeName;
133 _methodName = call.MethodName;
135 _methodSignature = (Type[]) call.MethodSignature;
136 _methodBase = call.MethodBase;
137 _callContext = call.LogicalCallContext;
141 internal virtual void InitMethodProperty(string key, object value)
145 case "__TypeName" : _typeName = (string) value; return;
146 case "__MethodName" : _methodName = (string) value; return;
147 case "__MethodSignature" : _methodSignature = (Type[]) value; return;
148 case "__Args" : _args = (object[]) value; return;
149 case "__CallContext" : _callContext = (LogicalCallContext) value; return;
150 case "__Uri" : _uri = (string) value; return;
152 case "__GenericArguments" : _genericArguments = (Type[]) value; return;
154 default: Properties[key] = value; return;
158 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
160 info.AddValue ("__TypeName", _typeName);
161 info.AddValue ("__MethodName", _methodName);
162 info.AddValue ("__MethodSignature", _methodSignature);
163 info.AddValue ("__Args", _args);
164 info.AddValue ("__CallContext", _callContext);
165 info.AddValue ("__Uri", _uri);
167 info.AddValue ("__GenericArguments", _genericArguments);
170 if (InternalProperties != null) {
171 foreach (DictionaryEntry entry in InternalProperties)
172 info.AddValue ((string) entry.Key, entry.Value);
176 public int ArgCount {
177 get { return _args.Length; }
180 public object[] Args {
181 get { return _args; }
184 public bool HasVarArgs {
185 get { return (MethodBase.CallingConvention | CallingConventions.VarArgs) != 0; }
188 public int InArgCount
192 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
193 return _inArgInfo.GetInOutArgCount();
197 public object[] InArgs
201 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
202 return _inArgInfo.GetInOutArgs (_args);
206 public LogicalCallContext LogicalCallContext {
208 if (_callContext == null)
209 _callContext = new LogicalCallContext ();
214 public MethodBase MethodBase {
216 if (_methodBase == null)
223 public string MethodName {
225 // lazily fill in _methodName from _methodBase
226 if (_methodName == null)
227 _methodName = _methodBase.Name;
232 public object MethodSignature {
234 if (_methodSignature == null && _methodBase != null)
236 ParameterInfo[] parameters = _methodBase.GetParameters();
237 _methodSignature = new Type[parameters.Length];
238 for (int n=0; n<parameters.Length; n++)
239 _methodSignature[n] = parameters[n].ParameterType;
241 return _methodSignature;
245 public virtual IDictionary Properties {
248 if (ExternalProperties == null) InitDictionary ();
249 return ExternalProperties;
253 internal virtual void InitDictionary()
255 MethodCallDictionary props = new MethodCallDictionary (this);
256 ExternalProperties = props;
257 InternalProperties = props.GetInternalProperties();
260 public string TypeName
263 // lazily fill in _typeName from _methodBase
264 if (_typeName == null)
265 _typeName = _methodBase.DeclaringType.AssemblyQualifiedName;
272 set { _uri = value; }
275 string IInternalMessage.Uri {
280 public object GetArg (int argNum)
282 return _args[argNum];
285 public string GetArgName (int index)
287 return _methodBase.GetParameters()[index].Name;
290 public object GetInArg (int argNum)
292 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
293 return _args[_inArgInfo.GetInOutArgIndex (argNum)];
296 public string GetInArgName (int index)
298 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
299 return _inArgInfo.GetInOutArgName(index);
303 public virtual object HeaderHandler (Header[] h)
305 throw new NotImplementedException ();
308 public virtual void Init ()
312 public void ResolveMethod ()
316 Type type = RemotingServices.GetServerTypeForUri (_uri);
318 string sname = _typeName != null ? " (" + _typeName + ")" : "";
319 throw new RemotingException ("Requested service not found" + sname + ". No receiver for uri " + _uri);
322 Type requestType = CastTo (_typeName, type);
323 if (requestType == null)
324 throw new RemotingException ("Cannot cast from client type '" + _typeName + "' to server type '" + type.FullName + "'");
326 // Look for the method in the requested type. The method signature is provided
327 // only if the method is overloaded in the requested type.
328 _methodBase = RemotingServices.GetMethodBaseFromName (requestType, _methodName, _methodSignature);
330 if (_methodBase == null)
331 throw new RemotingException ("Method " + _methodName + " not found in " + requestType);
333 // If the method is implemented in an interface, look for the method implementation.
334 // It can't be done in the previous GetMethodBaseFromName call because at that point we
335 // may not yet have the method signature.
336 if (requestType != type && requestType.IsInterface && !type.IsInterface) {
337 _methodBase = RemotingServices.GetVirtualMethod (type, _methodBase);
338 if (_methodBase == null)
339 throw new RemotingException ("Method " + _methodName + " not found in " + type);
343 _methodBase = RemotingServices.GetMethodBaseFromMethodMessage (this);
344 if (_methodBase == null) throw new RemotingException ("Method " + _methodName + " not found in " + TypeName);
349 if (_methodBase.IsGenericMethod && _methodBase.ContainsGenericParameters) {
350 if (GenericArguments == null)
351 throw new RemotingException ("The remoting infrastructure does not support open generic methods.");
352 _methodBase = ((MethodInfo) _methodBase).MakeGenericMethod (GenericArguments);
357 Type CastTo (string clientType, Type serverType)
359 clientType = GetTypeNameFromAssemblyQualifiedName (clientType);
360 if (clientType == serverType.FullName) return serverType;
362 // base class hierarchy
364 Type baseType = serverType.BaseType;
365 while (baseType != null) {
366 if (clientType == baseType.FullName) return baseType;
367 baseType = baseType.BaseType;
370 // Implemented interfaces
372 Type[] interfaces = serverType.GetInterfaces();
373 foreach (Type itype in interfaces)
374 if (clientType == itype.FullName) return itype;
379 static string GetTypeNameFromAssemblyQualifiedName (string aqname)
382 int p = aqname.IndexOf ("]]");
383 int i = aqname.IndexOf(',', p == -1 ? 0 : p + 2);
385 int i = aqname.IndexOf(',');
387 if (i != -1) aqname = aqname.Substring (0, i).Trim ();
392 public void RootSetObjectData (SerializationInfo info, StreamingContext context)
394 throw new NotImplementedException ();
397 Identity IInternalMessage.TargetIdentity
399 get { return _targetIdentity; }
400 set { _targetIdentity = value; }
404 public override string ToString ()
406 string s = _typeName.Split(',')[0] + "." + _methodName + " (";
409 for (int n=0; n<_args.Length; n++)
412 if (_args[n] != null) s += _args[n].GetType().Name + " ";
414 if (_args[n] != null) s += " = {" + _args[n] + "}";
424 Type[] GenericArguments {
426 if (_genericArguments != null)
427 return _genericArguments;
429 return _genericArguments = MethodBase.GetGenericArguments ();