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 protected IDictionary ExternalProperties
;
54 protected IDictionary InternalProperties
;
56 public MethodCall (Header
[] headers
)
60 if (headers
== null || headers
.Length
== 0) return;
62 foreach (Header header
in headers
)
63 InitMethodProperty (header
.Name
, header
.Value
);
68 internal MethodCall (SerializationInfo info
, StreamingContext context
)
72 foreach (SerializationEntry entry
in info
)
73 InitMethodProperty ((string)entry
.Name
, entry
.Value
);
76 internal MethodCall (CADMethodCallMessage msg
)
78 _typeName
= string.Copy (msg
.TypeName
);
79 _uri
= string.Copy (msg
.Uri
);
80 _methodName
= string.Copy (msg
.MethodName
);
82 // Get unmarshalled arguments
83 ArrayList args
= msg
.GetArguments ();
85 _args
= msg
.GetArgs (args
);
86 _methodSignature
= (Type
[]) msg
.GetMethodSignature (args
);
87 _callContext
= msg
.GetLogicalCallContext (args
);
88 if (_callContext
== null) _callContext
= new LogicalCallContext ();
93 if (msg
.PropertiesCount
> 0)
94 CADMessageBase
.UnmarshalProperties (Properties
, msg
.PropertiesCount
, args
);
97 public MethodCall (IMessage msg
)
99 if (msg
is IMethodMessage
)
100 CopyFrom ((IMethodMessage
) msg
);
103 IDictionary dic
= msg
.Properties
;
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
;
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;
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
);
161 if (InternalProperties
!= null) {
162 foreach (DictionaryEntry entry
in InternalProperties
)
163 info
.AddValue ((string) entry
.Key
, entry
.Value
);
167 public int ArgCount
{
168 get { return _args.Length; }
171 public object[] Args
{
172 get { return _args; }
175 public bool HasVarArgs
{
176 get { return (MethodBase.CallingConvention | CallingConventions.VarArgs) != 0; }
179 public int InArgCount
183 if (_inArgInfo
== null) _inArgInfo
= new ArgInfo (_methodBase
, ArgInfoType
.In
);
184 return _inArgInfo
.GetInOutArgCount();
188 public object[] InArgs
192 if (_inArgInfo
== null) _inArgInfo
= new ArgInfo (_methodBase
, ArgInfoType
.In
);
193 return _inArgInfo
.GetInOutArgs (_args
);
197 public LogicalCallContext LogicalCallContext
{
198 get { return _callContext; }
201 public MethodBase MethodBase
{
203 if (_methodBase
== null)
210 public string MethodName
{
211 get { return _methodName; }
214 public object MethodSignature
{
216 if (_methodSignature
== null && _methodBase
!= null)
218 ParameterInfo
[] parameters
= _methodBase
.GetParameters();
219 _methodSignature
= new Type
[parameters
.Length
];
220 for (int n
=0; n
<parameters
.Length
; n
++)
221 _methodSignature
[n
] = parameters
[n
].ParameterType
;
223 return _methodSignature
;
227 public virtual IDictionary Properties
{
230 if (ExternalProperties
== null) InitDictionary ();
231 return ExternalProperties
;
235 internal virtual void InitDictionary()
237 MethodCallDictionary props
= new MethodCallDictionary (this);
238 ExternalProperties
= props
;
239 InternalProperties
= props
.GetInternalProperties();
242 public string TypeName
244 get { return _typeName; }
249 set { _uri = value; }
252 public object GetArg (int argNum
)
254 return _args
[argNum
];
257 public string GetArgName (int index
)
259 return _methodBase
.GetParameters()[index
].Name
;
262 public object GetInArg (int argNum
)
264 if (_inArgInfo
== null) _inArgInfo
= new ArgInfo (_methodBase
, ArgInfoType
.In
);
265 return _args
[_inArgInfo
.GetInOutArgIndex (argNum
)];
268 public string GetInArgName (int index
)
270 if (_inArgInfo
== null) _inArgInfo
= new ArgInfo (_methodBase
, ArgInfoType
.In
);
271 return _inArgInfo
.GetInOutArgName(index
);
275 public virtual object HeaderHandler (Header
[] h
)
277 throw new NotImplementedException ();
280 public virtual void Init ()
284 public void ResolveMethod ()
288 Type type
= RemotingServices
.GetServerTypeForUri (_uri
);
289 if (type
== null) throw new RemotingException ("Requested service not found. No receiver for uri " + _uri
);
291 if (CanCastTo (_typeName
, type
)) {
292 _methodBase
= RemotingServices
.GetMethodBaseFromName (type
, _methodName
, _methodSignature
);
296 throw new RemotingException ("Cannot cast from client type '" + _typeName
+ "' to server type '" + type
.FullName
+ "'");
298 _methodBase
= RemotingServices
.GetMethodBaseFromMethodMessage (this);
301 bool CanCastTo (string clientType
, Type serverType
)
303 int i
= clientType
.IndexOf(',');
304 if (i
!= -1) clientType
= clientType
.Substring (0,i
).Trim();
306 if (clientType
== serverType
.FullName
) return true;
308 // base class hierarchy
310 Type baseType
= serverType
.BaseType
;
311 while (baseType
!= null) {
312 if (clientType
== baseType
.FullName
) return true;
313 baseType
= baseType
.BaseType
;
316 // Implemented interfaces
318 Type
[] interfaces
= serverType
.GetInterfaces();
319 foreach (Type itype
in interfaces
)
320 if (clientType
== itype
.FullName
) return true;
326 public void RootSetObjectData (SerializationInfo info
, StreamingContext context
)
328 throw new NotImplementedException ();
331 Identity IInternalMessage
.TargetIdentity
333 get { return _targetIdentity; }
334 set { _targetIdentity = value; }
337 public override string ToString ()
339 string s
= _typeName
.Split(',')[0] + "." + _methodName
+ " (";
340 Type
[] ts
= (Type
[]) MethodSignature
;
343 for (int n
=0; n
<_args
.Length
; n
++)
346 if (_args
[n
] != null) s
+= _args
[n
].GetType().Name
+ " ";
348 if (_args
[n
] != null) s
+= " = {" + _args[n] + "}";