2 // System.Web.Services.Protocols.LogicalMethodInfo.cs
5 // Miguel de Icaza (miguel@ximian.com)
6 // Tim Coleman (tim@timcoleman.com)
7 // Lluis Sanchez Gual (lluis@ximian.com)
9 // Copyright (C) Tim Coleman, 2002
10 // Copyright (C) Ximian, Inc, 2003
13 // BeginInvoke, EndInvoke are missing.
14 // AsyncResultParameter
17 // The reason for this class is so that it can cluster method/begin/end methods
18 // together, as the begin/end methods in generated files from WSDL does *NOT*
19 // contain all the information required to make a request.
21 // Either that, or the Begin*/End* versions probe the attributes on the regular
22 // method (which seems simpler).
26 // Permission is hereby granted, free of charge, to any person obtaining
27 // a copy of this software and associated documentation files (the
28 // "Software"), to deal in the Software without restriction, including
29 // without limitation the rights to use, copy, modify, merge, publish,
30 // distribute, sublicense, and/or sell copies of the Software, and to
31 // permit persons to whom the Software is furnished to do so, subject to
32 // the following conditions:
34 // The above copyright notice and this permission notice shall be
35 // included in all copies or substantial portions of the Software.
37 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
38 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
39 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
40 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
41 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
42 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
43 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46 using System
.Reflection
;
47 using System
.Collections
;
49 using System
.Web
.Services
;
51 namespace System
.Web
.Services
.Protocols
{
52 public sealed class LogicalMethodInfo
{
55 MethodInfo method_info
, end_method_info
;
56 ParameterInfo
[] parameters
;
57 ParameterInfo
[] out_parameters
;
58 ParameterInfo
[] in_parameters
;
59 WebMethodAttribute attribute
;
65 public LogicalMethodInfo (MethodInfo method_info
)
67 if (method_info
== null)
68 throw new ArgumentNullException ("method_info should be non-null");
69 if (method_info
.IsStatic
)
70 throw new InvalidOperationException ("method is static");
72 this.method_info
= method_info
;
76 // Only an internal contructor, called from "Create"
78 LogicalMethodInfo (MethodInfo method_info
, MethodInfo end_method_info
)
80 if (method_info
== null)
81 throw new ArgumentNullException ("method_info should be non-null");
82 if (method_info
.IsStatic
)
83 throw new InvalidOperationException ("method is static");
85 this.method_info
= method_info
;
86 this.end_method_info
= end_method_info
;
89 #endregion // Constructors
94 // Signatures for Begin/End methods:
96 // public System.IAsyncResult BeginHelloWorld(ARG1, ARG2, System.AsyncCallback callback, object asyncState) {
97 // public string EndHelloWorld(System.IAsyncResult asyncResult) {
99 public ParameterInfo AsyncCallbackParameter
{
101 ParameterInfo
[] pi
= method_info
.GetParameters ();
102 return pi
[pi
.Length
-2];
106 public ParameterInfo AsyncResultParameter
{
108 ParameterInfo
[] pi
= end_method_info
.GetParameters ();
109 return pi
[pi
.Length
-1];
113 public ParameterInfo AsyncStateParameter
{
115 ParameterInfo
[] pi
= method_info
.GetParameters ();
116 return pi
[pi
.Length
-1];
120 public MethodInfo BeginMethodInfo
{
122 if (IsBeginMethod (method_info
))
128 public ICustomAttributeProvider CustomAttributeProvider
{
134 public Type DeclaringType
{
136 return method_info
.DeclaringType
;
140 public MethodInfo EndMethodInfo
{
142 return end_method_info
;
146 public ParameterInfo
[] InParameters
{
148 if (parameters
== null)
149 ComputeParameters ();
150 return in_parameters
;
154 public bool IsAsync
{
156 return end_method_info
!= null;
162 return ReturnType
== typeof (void);
166 public MethodInfo MethodInfo
{
168 if (IsBeginMethod (method_info
))
176 return method_info
.Name
;
180 internal MethodInfo ActualMethodInfo
{
181 get { return method_info; }
184 void ComputeParameters ()
186 ParameterInfo
[] pars
= method_info
.GetParameters ();
189 parameters
= new ParameterInfo
[pars
.Length
- 2];
190 Array
.Copy (pars
, 0, parameters
, 0, pars
.Length
- 2);
191 in_parameters
= new ParameterInfo
[parameters
.Length
];
192 parameters
.CopyTo (in_parameters
, 0);
194 ParameterInfo
[] outPars
= end_method_info
.GetParameters ();
195 out_parameters
= new ParameterInfo
[outPars
.Length
- 1];
196 Array
.Copy (outPars
, 0, out_parameters
, 0, out_parameters
.Length
);
204 foreach (ParameterInfo p
in parameters
){
205 Type ptype
= p
.ParameterType
;
213 out_parameters
= new ParameterInfo
[out_count
];
215 for (int j
= 0; j
< parameters
.Length
; j
++){
216 if (parameters
[j
].ParameterType
.IsByRef
)
217 out_parameters
[i
++] = parameters
[j
];
219 in_parameters
= new ParameterInfo
[in_count
];
221 for (int j
= 0; j
< parameters
.Length
; j
++){
222 if (parameters
[j
].ParameterType
.IsByRef
){
223 if (!parameters
[j
].IsOut
)
224 in_parameters
[i
++] = parameters
[j
];
226 in_parameters
[i
++] = parameters
[j
];
231 public ParameterInfo
[] OutParameters
{
233 if (parameters
== null)
234 ComputeParameters ();
235 return out_parameters
;
239 public ParameterInfo
[] Parameters
{
241 if (parameters
== null)
242 ComputeParameters ();
247 public Type ReturnType
{
250 return end_method_info
.ReturnType
;
252 return method_info
.ReturnType
;
256 public ICustomAttributeProvider ReturnTypeCustomAttributeProvider
{
258 return method_info
.ReturnTypeCustomAttributes
;
262 internal bool EnableSession
{
264 if (method_info
== null)
267 if (attribute
== null) {
268 object [] o
= method_info
.GetCustomAttributes (false);
269 foreach (Attribute att
in o
) {
270 if (att
is WebMethodAttribute
) {
271 attribute
= (WebMethodAttribute
) att
;
277 return (attribute
!= null) ? attribute
.EnableSession
: false;
280 internal int CacheDuration
{
282 if (method_info
== null)
285 if (attribute
== null) {
286 object [] o
= method_info
.GetCustomAttributes (false);
287 foreach (Attribute att
in o
) {
288 if (att
is WebMethodAttribute
) {
289 attribute
= (WebMethodAttribute
) att
;
295 return (attribute
!= null) ? attribute
.CacheDuration
: -1;
298 #endregion // Properties
302 public IAsyncResult
BeginInvoke (object target
, object[] values
, AsyncCallback callback
, object asyncState
)
304 int len
= (values
!=null) ? values
.Length
: 0;
305 object[] pars
= new object [len
+ 2];
308 values
.CopyTo (pars
, 0);
310 pars
[len
] = callback
;
311 pars
[len
+1] = asyncState
;
313 return (IAsyncResult
) method_info
.Invoke (target
, pars
);
316 public static LogicalMethodInfo
[] Create (MethodInfo
[] method_infos
)
318 return Create (method_infos
, LogicalMethodTypes
.Sync
| LogicalMethodTypes
.Async
);
321 public static LogicalMethodInfo
[] Create (MethodInfo
[] method_infos
, LogicalMethodTypes types
)
323 ArrayList sync
= ((types
& LogicalMethodTypes
.Sync
) != 0) ? new ArrayList () : null;
324 ArrayList begin
, end
;
326 if ((types
& LogicalMethodTypes
.Async
) != 0){
327 begin
= new ArrayList ();
328 end
= new ArrayList ();
332 foreach (MethodInfo mi
in method_infos
){
333 if (IsBeginMethod (mi
) && begin
!= null)
335 else if (IsEndMethod (mi
) && end
!= null)
337 else if (sync
!= null)
341 int bcount
= 0, count
= 0;
343 bcount
= count
= begin
.Count
;
344 if (count
!= end
.Count
)
345 throw new InvalidOperationException ("Imbalance of begin/end methods");
350 LogicalMethodInfo
[] res
= new LogicalMethodInfo
[count
];
353 foreach (MethodInfo bm
in begin
){
354 string end_name
= "End" + bm
.Name
.Substring (5);
356 for (int i
= 0; i
< bcount
; i
++){
357 MethodInfo em
= (MethodInfo
) end
[i
];
358 if (em
.Name
== end_name
){
359 res
[dest
++] = new LogicalMethodInfo (bm
, em
);
362 throw new InvalidOperationException ("Imbalance of begin/end methods");
369 foreach (MethodInfo mi
in sync
){
370 res
[dest
++] = new LogicalMethodInfo (mi
);
376 public object[] EndInvoke (object target
, IAsyncResult asyncResult
)
378 if (parameters
== null)
379 ComputeParameters ();
381 object[] values
= new object [out_parameters
.Length
+ 1];
382 values
[values
.Length
- 1] = asyncResult
;
383 object res
= end_method_info
.Invoke (target
, values
);
385 int retc
= IsVoid
? 0 : 1;
386 object [] ret
= new object [retc
+ out_parameters
.Length
];
388 if (retc
== 1) ret
[0] = res
;
390 Array
.Copy (values
, 0, ret
, retc
, out_parameters
.Length
);
394 public object GetCustomAttribute (Type type
)
396 return Attribute
.GetCustomAttribute (method_info
, type
, false);
399 public object[] GetCustomAttributes (Type type
)
401 return method_info
.GetCustomAttributes (type
, false);
404 public object[] Invoke (object target
, object[] values
)
406 if (parameters
== null)
407 ComputeParameters ();
409 int retc
= IsVoid
? 0 : 1;
410 object [] ret
= new object [retc
+ out_parameters
.Length
];
411 object res
= method_info
.Invoke (target
, values
);
412 if (retc
== 1) ret
[0] = res
;
415 for (int i
= 0; i
< parameters
.Length
; i
++){
416 if (parameters
[i
].ParameterType
.IsByRef
)
417 ret
[j
++] = values
[i
];
423 public static bool IsBeginMethod (MethodInfo method_info
)
425 if (method_info
== null)
426 throw new ArgumentNullException ("method_info can not be null");
428 if (method_info
.ReturnType
!= typeof (IAsyncResult
))
431 if (method_info
.Name
.StartsWith ("Begin"))
437 public static bool IsEndMethod (MethodInfo method_info
)
439 if (method_info
== null)
440 throw new ArgumentNullException ("method_info can not be null");
442 ParameterInfo
[] parameter_info
= method_info
.GetParameters ();
443 if (parameter_info
.Length
!= 1)
445 if (parameter_info
[0].ParameterType
!= typeof (IAsyncResult
))
447 if (method_info
.Name
.StartsWith ("End"))
453 public override string ToString ()
455 StringBuilder sb
= new StringBuilder ();
456 if (parameters
== null)
457 ComputeParameters ();
459 for (int i
= 0; i
< parameters
.Length
; i
++){
460 sb
.Append (parameters
[i
].ParameterType
);
461 if (parameters
[i
].ParameterType
.IsByRef
)
462 sb
.Append (" ByRef");
464 if (i
+1 != parameters
.Length
)
468 return String
.Format (
470 method_info
.ReturnType
, method_info
.Name
,
474 #endregion // Methods