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.end_method_info
= end_method_info
;
88 #endregion // Constructors
93 // Signatures for Begin/End methods:
95 // public System.IAsyncResult BeginHelloWorld(ARG1, ARG2, System.AsyncCallback callback, object asyncState) {
96 // public string EndHelloWorld(System.IAsyncResult asyncResult) {
98 public ParameterInfo AsyncCallbackParameter
{
100 ParameterInfo
[] pi
= method_info
.GetParameters ();
101 return pi
[pi
.Length
-2];
105 public ParameterInfo AsyncResultParameter
{
107 ParameterInfo
[] pi
= end_method_info
.GetParameters ();
108 return pi
[pi
.Length
-1];
112 public ParameterInfo AsyncStateParameter
{
114 ParameterInfo
[] pi
= method_info
.GetParameters ();
115 return pi
[pi
.Length
-1];
119 public MethodInfo BeginMethodInfo
{
121 if (IsBeginMethod (method_info
))
127 public ICustomAttributeProvider CustomAttributeProvider
{
133 public Type DeclaringType
{
135 return method_info
.DeclaringType
;
139 public MethodInfo EndMethodInfo
{
141 return end_method_info
;
145 public ParameterInfo
[] InParameters
{
147 if (parameters
== null)
148 ComputeParameters ();
149 return in_parameters
;
153 public bool IsAsync
{
155 return end_method_info
!= null;
161 return ReturnType
== typeof (void);
165 public MethodInfo MethodInfo
{
173 return method_info
.Name
;
177 void ComputeParameters ()
179 ParameterInfo
[] pars
= method_info
.GetParameters ();
182 parameters
= new ParameterInfo
[pars
.Length
- 2];
183 Array
.Copy (pars
, 0, parameters
, 0, pars
.Length
- 2);
184 in_parameters
= new ParameterInfo
[parameters
.Length
];
185 parameters
.CopyTo (in_parameters
, 0);
187 ParameterInfo
[] outPars
= end_method_info
.GetParameters ();
188 out_parameters
= new ParameterInfo
[outPars
.Length
- 1];
189 Array
.Copy (outPars
, 0, out_parameters
, 0, out_parameters
.Length
);
197 foreach (ParameterInfo p
in parameters
){
198 Type ptype
= p
.ParameterType
;
206 out_parameters
= new ParameterInfo
[out_count
];
208 for (int j
= 0; j
< parameters
.Length
; j
++){
209 if (parameters
[j
].ParameterType
.IsByRef
)
210 out_parameters
[i
++] = parameters
[j
];
212 in_parameters
= new ParameterInfo
[in_count
];
214 for (int j
= 0; j
< parameters
.Length
; j
++){
215 if (parameters
[j
].ParameterType
.IsByRef
){
216 if (!parameters
[j
].IsOut
)
217 in_parameters
[i
++] = parameters
[j
];
219 in_parameters
[i
++] = parameters
[j
];
224 public ParameterInfo
[] OutParameters
{
226 if (parameters
== null)
227 ComputeParameters ();
228 return out_parameters
;
232 public ParameterInfo
[] Parameters
{
234 if (parameters
== null)
235 ComputeParameters ();
240 public Type ReturnType
{
243 return end_method_info
.ReturnType
;
245 return method_info
.ReturnType
;
249 public ICustomAttributeProvider ReturnTypeCustomAttributeProvider
{
251 return method_info
.ReturnTypeCustomAttributes
;
255 internal bool EnableSession
{
257 if (method_info
== null)
260 if (attribute
== null) {
261 object [] o
= method_info
.GetCustomAttributes (false);
262 foreach (Attribute att
in o
) {
263 if (att
is WebMethodAttribute
) {
264 attribute
= (WebMethodAttribute
) att
;
270 return (attribute
!= null) ? attribute
.EnableSession
: false;
273 #endregion // Properties
277 public IAsyncResult
BeginInvoke (object target
, object[] values
, AsyncCallback callback
, object asyncState
)
279 int len
= (values
!=null) ? values
.Length
: 0;
280 object[] pars
= new object [len
+ 2];
283 values
.CopyTo (pars
, 0);
285 pars
[len
] = callback
;
286 pars
[len
+1] = asyncState
;
288 return (IAsyncResult
) method_info
.Invoke (target
, pars
);
291 public static LogicalMethodInfo
[] Create (MethodInfo
[] method_infos
)
293 return Create (method_infos
, LogicalMethodTypes
.Sync
| LogicalMethodTypes
.Async
);
296 public static LogicalMethodInfo
[] Create (MethodInfo
[] method_infos
, LogicalMethodTypes types
)
298 ArrayList sync
= ((types
& LogicalMethodTypes
.Sync
) != 0) ? new ArrayList () : null;
299 ArrayList begin
, end
;
301 if ((types
& LogicalMethodTypes
.Async
) != 0){
302 begin
= new ArrayList ();
303 end
= new ArrayList ();
307 foreach (MethodInfo mi
in method_infos
){
308 if (IsBeginMethod (mi
) && begin
!= null)
310 else if (IsEndMethod (mi
) && end
!= null)
312 else if (sync
!= null)
316 int bcount
= 0, count
= 0;
318 bcount
= count
= begin
.Count
;
319 if (count
!= end
.Count
)
320 throw new InvalidOperationException ("Imbalance of begin/end methods");
325 LogicalMethodInfo
[] res
= new LogicalMethodInfo
[count
];
328 foreach (MethodInfo bm
in begin
){
329 string end_name
= "End" + bm
.Name
.Substring (5);
331 for (int i
= 0; i
< bcount
; i
++){
332 MethodInfo em
= (MethodInfo
) end
[i
];
333 if (em
.Name
== end_name
){
334 res
[dest
++] = new LogicalMethodInfo (bm
, em
);
337 throw new InvalidOperationException ("Imbalance of begin/end methods");
344 foreach (MethodInfo mi
in sync
){
345 res
[dest
++] = new LogicalMethodInfo (mi
);
351 public object[] EndInvoke (object target
, IAsyncResult asyncResult
)
353 if (parameters
== null)
354 ComputeParameters ();
356 object[] values
= new object [out_parameters
.Length
+ 1];
357 values
[values
.Length
- 1] = asyncResult
;
358 object res
= end_method_info
.Invoke (target
, values
);
360 int retc
= IsVoid
? 0 : 1;
361 object [] ret
= new object [retc
+ out_parameters
.Length
];
363 if (retc
== 1) ret
[0] = res
;
365 Array
.Copy (values
, 0, ret
, retc
, out_parameters
.Length
);
369 public object GetCustomAttribute (Type type
)
371 return Attribute
.GetCustomAttribute (method_info
, type
, true);
374 public object[] GetCustomAttributes (Type type
)
376 return method_info
.GetCustomAttributes (type
, true);
379 public object[] Invoke (object target
, object[] values
)
381 if (parameters
== null)
382 ComputeParameters ();
384 int retc
= IsVoid
? 0 : 1;
385 object [] ret
= new object [retc
+ out_parameters
.Length
];
386 object res
= method_info
.Invoke (target
, values
);
387 if (retc
== 1) ret
[0] = res
;
390 for (int i
= 0; i
< parameters
.Length
; i
++){
391 if (parameters
[i
].ParameterType
.IsByRef
)
392 ret
[j
++] = values
[i
];
398 public static bool IsBeginMethod (MethodInfo method_info
)
400 if (method_info
== null)
401 throw new ArgumentNullException ("method_info can not be null");
403 if (method_info
.ReturnType
!= typeof (IAsyncResult
))
406 if (method_info
.Name
.StartsWith ("Begin"))
412 public static bool IsEndMethod (MethodInfo method_info
)
414 if (method_info
== null)
415 throw new ArgumentNullException ("method_info can not be null");
417 ParameterInfo
[] parameter_info
= method_info
.GetParameters ();
418 if (parameter_info
.Length
!= 1)
420 if (parameter_info
[0].ParameterType
!= typeof (IAsyncResult
))
422 if (method_info
.Name
.StartsWith ("End"))
428 public override string ToString ()
430 StringBuilder sb
= new StringBuilder ();
431 if (parameters
== null)
432 ComputeParameters ();
434 for (int i
= 0; i
< parameters
.Length
; i
++){
435 sb
.Append (parameters
[i
].ParameterType
);
436 if (parameters
[i
].ParameterType
.IsByRef
)
437 sb
.Append (" ByRef");
439 if (i
+1 != parameters
.Length
)
443 return String
.Format (
445 method_info
.ReturnType
, method_info
.Name
,
449 #endregion // Methods