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 public class MethodCall : IMethodCallMessage, IMethodMessage, IMessage, ISerializable, IInternalMessage, ISerializationRootObject
47 Type[] _methodSignature;
48 MethodBase _methodBase;
49 LogicalCallContext _callContext;
51 Identity _targetIdentity;
53 Type[] _genericArguments;
56 protected IDictionary ExternalProperties;
57 protected IDictionary InternalProperties;
59 public MethodCall (Header [] headers)
63 if (headers == null || headers.Length == 0) return;
65 foreach (Header header in headers)
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 ()
126 internal void CopyFrom (IMethodMessage call)
129 _typeName = call.TypeName;
130 _methodName = call.MethodName;
132 _methodSignature = (Type[]) call.MethodSignature;
133 _methodBase = call.MethodBase;
134 _callContext = call.LogicalCallContext;
138 internal virtual void InitMethodProperty(string key, object value)
142 case "__TypeName" : _typeName = (string) value; return;
143 case "__MethodName" : _methodName = (string) value; return;
144 case "__MethodSignature" : _methodSignature = (Type[]) value; return;
145 case "__Args" : _args = (object[]) value; return;
146 case "__CallContext" : _callContext = (LogicalCallContext) value; return;
147 case "__Uri" : _uri = (string) value; return;
149 case "__GenericArguments" : _genericArguments = (Type[]) value; return;
151 default: Properties[key] = value; return;
155 public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
157 info.AddValue ("__TypeName", _typeName);
158 info.AddValue ("__MethodName", _methodName);
159 info.AddValue ("__MethodSignature", _methodSignature);
160 info.AddValue ("__Args", _args);
161 info.AddValue ("__CallContext", _callContext);
162 info.AddValue ("__Uri", _uri);
164 info.AddValue ("__GenericArguments", _genericArguments);
167 if (InternalProperties != null) {
168 foreach (DictionaryEntry entry in InternalProperties)
169 info.AddValue ((string) entry.Key, entry.Value);
173 public int ArgCount {
174 get { return _args.Length; }
177 public object[] Args {
178 get { return _args; }
181 public bool HasVarArgs {
182 get { return (MethodBase.CallingConvention | CallingConventions.VarArgs) != 0; }
185 public int InArgCount
189 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
190 return _inArgInfo.GetInOutArgCount();
194 public object[] InArgs
198 if (_inArgInfo == null) _inArgInfo = new ArgInfo (_methodBase, ArgInfoType.In);
199 return _inArgInfo.GetInOutArgs (_args);
203 public LogicalCallContext LogicalCallContext {
205 if (_callContext == null)
206 _callContext = new LogicalCallContext ();
211 public MethodBase MethodBase {
213 if (_methodBase == null)
220 public string MethodName {
222 // lazily fill in _methodName from _methodBase
223 if (_methodName == null)
224 _methodName = _methodBase.Name;
229 public object MethodSignature {
231 if (_methodSignature == null && _methodBase != null)
233 ParameterInfo[] parameters = _methodBase.GetParameters();
234 _methodSignature = new Type[parameters.Length];
235 for (int n=0; n<parameters.Length; n++)
236 _methodSignature[n] = parameters[n].ParameterType;
238 return _methodSignature;
242 public virtual IDictionary Properties {
245 if (ExternalProperties == null) InitDictionary ();
246 return ExternalProperties;
250 internal virtual void InitDictionary()
252 MethodCallDictionary props = new MethodCallDictionary (this);
253 ExternalProperties = props;
254 InternalProperties = props.GetInternalProperties();
257 public string TypeName
260 // lazily fill in _typeName from _methodBase
261 if (_typeName == null)
262 _typeName = _methodBase.DeclaringType.AssemblyQualifiedName;
269 set { _uri = value; }
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 if (CastTo (_typeName, type) != null) {
315 _methodBase = RemotingServices.GetMethodBaseFromName (type, _methodName, _methodSignature);
316 if (_methodBase == null) throw new RemotingException ("Method " + _methodName + " not found in " + type);
319 throw new RemotingException ("Cannot cast from client type '" + _typeName + "' to server type '" + type.FullName + "'");
321 _methodBase = RemotingServices.GetMethodBaseFromMethodMessage (this);
322 if (_methodBase == null) throw new RemotingException ("Method " + _methodName + " not found in " + TypeName);
327 if (_methodBase.IsGenericMethod && _methodBase.ContainsGenericParameters) {
328 if (GenericArguments == null)
329 throw new RemotingException ("The remoting infrastructure does not support open generic methods.");
330 _methodBase = ((MethodInfo) _methodBase).MakeGenericMethod (GenericArguments);
336 Type CastTo (string clientType, Type serverType)
338 clientType = GetTypeNameFromAssemblyQualifiedName (clientType);
339 if (clientType == serverType.FullName) return serverType;
341 // base class hierarchy
343 Type baseType = serverType.BaseType;
344 while (baseType != null) {
345 if (clientType == baseType.FullName) return baseType;
346 baseType = baseType.BaseType;
349 // Implemented interfaces
351 Type[] interfaces = serverType.GetInterfaces();
352 foreach (Type itype in interfaces)
353 if (clientType == itype.FullName) return itype;
358 static string GetTypeNameFromAssemblyQualifiedName (string aqname)
361 int p = aqname.IndexOf ("]]");
362 int i = aqname.IndexOf(',', p == -1 ? 0 : p + 2);
364 int i = aqname.IndexOf(',');
366 if (i != -1) aqname = aqname.Substring (0, i).Trim ();
371 public void RootSetObjectData (SerializationInfo info, StreamingContext context)
373 throw new NotImplementedException ();
376 Identity IInternalMessage.TargetIdentity
378 get { return _targetIdentity; }
379 set { _targetIdentity = value; }
382 public override string ToString ()
384 string s = _typeName.Split(',')[0] + "." + _methodName + " (";
387 for (int n=0; n<_args.Length; n++)
390 if (_args[n] != null) s += _args[n].GetType().Name + " ";
392 if (_args[n] != null) s += " = {" + _args[n] + "}";
401 Type[] GenericArguments {
403 if (_genericArguments != null)
404 return _genericArguments;
406 return _genericArguments = MethodBase.GetGenericArguments ();