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)]
41 [System.Runtime.InteropServices.ComVisible (true)]
42 public class MethodCall : IMethodCallMessage, IMethodMessage, IMessage, ISerializable, IInternalMessage, ISerializationRootObject
48 Type[] _methodSignature;
49 MethodBase _methodBase;
50 LogicalCallContext _callContext;
52 Identity _targetIdentity;
53 Type[] _genericArguments;
55 protected IDictionary ExternalProperties;
56 protected IDictionary InternalProperties;
58 public MethodCall (Header [] h1)
62 if (h1 == null || h1.Length == 0) return;
64 foreach (Header header in h1)
65 InitMethodProperty (header.Name, header.Value);
70 internal MethodCall (SerializationInfo info, StreamingContext context)
74 foreach (SerializationEntry entry in info)
75 InitMethodProperty ((string)entry.Name, entry.Value);
78 internal MethodCall (CADMethodCallMessage msg)
80 _uri = string.Copy (msg.Uri);
82 // Get unmarshalled arguments
83 ArrayList args = msg.GetArguments ();
85 _args = msg.GetArgs (args);
86 _callContext = msg.GetLogicalCallContext (args);
87 if (_callContext == null)
88 _callContext = new LogicalCallContext ();
90 _methodBase = msg.GetMethod ();
94 if (msg.PropertiesCount > 0)
95 CADMessageBase.UnmarshalProperties (Properties, msg.PropertiesCount, args);
98 public MethodCall (IMessage msg)
100 if (msg is IMethodMessage)
101 CopyFrom ((IMethodMessage) msg);
104 foreach (DictionaryEntry entry in msg.Properties)
105 InitMethodProperty ((String) entry.Key, entry.Value);
110 internal MethodCall (string uri, string typeName, string methodName, object[] args)
113 _typeName = typeName;
114 _methodName = methodName;
121 internal MethodCall ()
125 internal void CopyFrom (IMethodMessage call)
128 _typeName = call.TypeName;
129 _methodName = call.MethodName;
131 _methodSignature = (Type[]) call.MethodSignature;
132 _methodBase = call.MethodBase;
133 _callContext = call.LogicalCallContext;
137 internal virtual void InitMethodProperty(string key, object value)
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;
152 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
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);
162 if (InternalProperties != null) {
163 foreach (DictionaryEntry entry in InternalProperties)
164 info.AddValue ((string) entry.Key, entry.Value);
168 public int ArgCount {
169 get { return _args.Length; }
172 public object[] Args {
173 get { return _args; }
176 public bool HasVarArgs {
177 get { return (MethodBase.CallingConvention | CallingConventions.VarArgs) != 0; }
180 public int InArgCount
184 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
185 return _inArgInfo.GetInOutArgCount();
189 public object[] InArgs
193 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
194 return _inArgInfo.GetInOutArgs (_args);
198 public LogicalCallContext LogicalCallContext {
200 if (_callContext == null)
201 _callContext = new LogicalCallContext ();
206 public MethodBase MethodBase {
208 if (_methodBase == null)
215 public string MethodName {
217 // lazily fill in _methodName from _methodBase
218 if (_methodName == null)
219 _methodName = _methodBase.Name;
224 public object MethodSignature {
226 if (_methodSignature == null && _methodBase != null)
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;
233 return _methodSignature;
237 public virtual IDictionary Properties {
240 if (ExternalProperties == null) InitDictionary ();
241 return ExternalProperties;
245 internal virtual void InitDictionary()
247 MethodCallDictionary props = new MethodCallDictionary (this);
248 ExternalProperties = props;
249 InternalProperties = props.GetInternalProperties();
252 public string TypeName
255 // lazily fill in _typeName from _methodBase
256 if (_typeName == null)
257 _typeName = _methodBase.DeclaringType.AssemblyQualifiedName;
264 set { _uri = value; }
267 string IInternalMessage.Uri {
272 public object GetArg (int argNum)
274 return _args[argNum];
277 public string GetArgName (int index)
279 return _methodBase.GetParameters()[index].Name;
282 public object GetInArg (int argNum)
284 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
285 return _args[_inArgInfo.GetInOutArgIndex (argNum)];
288 public string GetInArgName (int index)
290 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
291 return _inArgInfo.GetInOutArgName(index);
295 public virtual object HeaderHandler (Header[] h)
297 throw new NotImplementedException ();
300 public virtual void Init ()
304 public void ResolveMethod ()
308 Type type = RemotingServices.GetServerTypeForUri (_uri);
310 string sname = _typeName != null ? " (" + _typeName + ")" : "";
311 throw new RemotingException ("Requested service not found" + sname + ". No receiver for uri " + _uri);
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 + "'");
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);
322 if (_methodBase == null)
323 throw new RemotingException ("Method " + _methodName + " not found in " + requestType);
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);
335 _methodBase = RemotingServices.GetMethodBaseFromMethodMessage (this);
336 if (_methodBase == null) throw new RemotingException ("Method " + _methodName + " not found in " + TypeName);
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);
347 Type CastTo (string clientType, Type serverType)
349 clientType = GetTypeNameFromAssemblyQualifiedName (clientType);
350 if (clientType == serverType.FullName) return serverType;
352 // base class hierarchy
354 Type baseType = serverType.BaseType;
355 while (baseType != null) {
356 if (clientType == baseType.FullName) return baseType;
357 baseType = baseType.BaseType;
360 // Implemented interfaces
362 Type[] interfaces = serverType.GetInterfaces();
363 foreach (Type itype in interfaces)
364 if (clientType == itype.FullName) return itype;
369 static string GetTypeNameFromAssemblyQualifiedName (string aqname)
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 ();
378 public void RootSetObjectData (SerializationInfo info, StreamingContext ctx)
380 throw new NotImplementedException ();
383 Identity IInternalMessage.TargetIdentity
385 get { return _targetIdentity; }
386 set { _targetIdentity = value; }
389 Type[] GenericArguments {
391 if (_genericArguments != null)
392 return _genericArguments;
394 return _genericArguments = MethodBase.GetGenericArguments ();