2 // System.Web.Services.Protocols.LogicalMethodInfo.cs
\r
5 // Miguel de Icaza (miguel@ximian.com)
\r
6 // Tim Coleman (tim@timcoleman.com)
\r
7 // Lluis Sanchez Gual (lluis@ximian.com)
\r
9 // Copyright (C) Tim Coleman, 2002
\r
10 // Copyright (C) Ximian, Inc, 2003
\r
13 // BeginInvoke, EndInvoke are missing.
\r
14 // AsyncResultParameter
\r
17 // The reason for this class is so that it can cluster method/begin/end methods
\r
18 // together, as the begin/end methods in generated files from WSDL does *NOT*
\r
19 // contain all the information required to make a request.
\r
21 // Either that, or the Begin*/End* versions probe the attributes on the regular
\r
22 // method (which seems simpler).
\r
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;
\r
47 using System.Collections;
\r
49 using System.Web.Services;
\r
51 namespace System.Web.Services.Protocols {
\r
52 public sealed class LogicalMethodInfo {
\r
55 MethodInfo method_info, end_method_info;
\r
56 ParameterInfo [] parameters;
\r
57 ParameterInfo [] out_parameters;
\r
58 ParameterInfo [] in_parameters;
\r
59 WebMethodAttribute attribute;
\r
61 #endregion // Fields.
\r
63 #region Constructors
\r
65 public LogicalMethodInfo (MethodInfo method_info)
\r
67 if (method_info == null)
\r
68 throw new ArgumentNullException ("method_info should be non-null");
\r
69 if (method_info.IsStatic)
\r
70 throw new InvalidOperationException ("method is static");
\r
72 this.method_info = method_info;
\r
76 // Only an internal contructor, called from "Create"
\r
78 LogicalMethodInfo (MethodInfo method_info, MethodInfo end_method_info)
\r
80 if (method_info == null)
\r
81 throw new ArgumentNullException ("method_info should be non-null");
\r
82 if (method_info.IsStatic)
\r
83 throw new InvalidOperationException ("method is static");
\r
85 this.end_method_info = end_method_info;
\r
88 #endregion // Constructors
\r
93 // Signatures for Begin/End methods:
\r
95 // public System.IAsyncResult BeginHelloWorld(ARG1, ARG2, System.AsyncCallback callback, object asyncState) {
\r
96 // public string EndHelloWorld(System.IAsyncResult asyncResult) {
\r
98 public ParameterInfo AsyncCallbackParameter {
\r
100 ParameterInfo [] pi = method_info.GetParameters ();
\r
101 return pi [pi.Length-2];
\r
105 public ParameterInfo AsyncResultParameter {
\r
107 ParameterInfo [] pi = end_method_info.GetParameters ();
\r
108 return pi [pi.Length-1];
\r
112 public ParameterInfo AsyncStateParameter {
\r
114 ParameterInfo [] pi = method_info.GetParameters ();
\r
115 return pi [pi.Length-1];
\r
119 public MethodInfo BeginMethodInfo {
\r
121 if (IsBeginMethod (method_info))
\r
122 return method_info;
\r
127 public ICustomAttributeProvider CustomAttributeProvider {
\r
129 return method_info;
\r
133 public Type DeclaringType {
\r
135 return method_info.DeclaringType;
\r
139 public MethodInfo EndMethodInfo {
\r
141 return end_method_info;
\r
145 public ParameterInfo[] InParameters {
\r
147 if (parameters == null)
\r
148 ComputeParameters ();
\r
149 return in_parameters;
\r
153 public bool IsAsync {
\r
155 return end_method_info != null;
\r
159 public bool IsVoid {
\r
161 return ReturnType == typeof (void);
\r
165 public MethodInfo MethodInfo {
\r
167 return method_info;
\r
171 public string Name {
\r
173 return method_info.Name;
\r
177 void ComputeParameters ()
\r
179 ParameterInfo[] pars = method_info.GetParameters ();
\r
182 parameters = new ParameterInfo [pars.Length - 2];
\r
183 Array.Copy (pars, 0, parameters, 0, pars.Length - 2);
\r
184 in_parameters = new ParameterInfo [parameters.Length];
\r
185 parameters.CopyTo (in_parameters, 0);
\r
187 ParameterInfo[] outPars = end_method_info.GetParameters ();
\r
188 out_parameters = new ParameterInfo [outPars.Length - 1];
\r
189 Array.Copy (outPars, 0, out_parameters, 0, out_parameters.Length);
\r
197 foreach (ParameterInfo p in parameters){
\r
198 Type ptype = p.ParameterType;
\r
199 if (ptype.IsByRef){
\r
206 out_parameters = new ParameterInfo [out_count];
\r
208 for (int j = 0; j < parameters.Length; j++){
\r
209 if (parameters [j].ParameterType.IsByRef)
\r
210 out_parameters [i++] = parameters [j];
\r
212 in_parameters = new ParameterInfo [in_count];
\r
214 for (int j = 0; j < parameters.Length; j++){
\r
215 if (parameters [j].ParameterType.IsByRef){
\r
216 if (!parameters [j].IsOut)
\r
217 in_parameters [i++] = parameters [j];
\r
219 in_parameters [i++] = parameters [j];
\r
224 public ParameterInfo[] OutParameters {
\r
226 if (parameters == null)
\r
227 ComputeParameters ();
\r
228 return out_parameters;
\r
232 public ParameterInfo[] Parameters {
\r
234 if (parameters == null)
\r
235 ComputeParameters ();
\r
240 public Type ReturnType {
\r
243 return end_method_info.ReturnType;
\r
245 return method_info.ReturnType;
\r
249 public ICustomAttributeProvider ReturnTypeCustomAttributeProvider {
\r
251 return method_info.ReturnTypeCustomAttributes;
\r
255 internal bool EnableSession {
\r
257 if (method_info == null)
\r
260 if (attribute == null) {
\r
261 object [] o = method_info.GetCustomAttributes (false);
\r
262 foreach (Attribute att in o) {
\r
263 if (att is WebMethodAttribute) {
\r
264 attribute = (WebMethodAttribute) att;
\r
270 return (attribute != null) ? attribute.EnableSession : false;
\r
273 #endregion // Properties
\r
277 public IAsyncResult BeginInvoke (object target, object[] values, AsyncCallback callback, object asyncState)
\r
279 int len = (values!=null) ? values.Length : 0;
\r
280 object[] pars = new object [len + 2];
\r
283 values.CopyTo (pars, 0);
\r
285 pars [len] = callback;
\r
286 pars [len+1] = asyncState;
\r
288 return (IAsyncResult) method_info.Invoke (target, pars);
\r
291 public static LogicalMethodInfo[] Create (MethodInfo[] method_infos)
\r
293 return Create (method_infos, LogicalMethodTypes.Sync | LogicalMethodTypes.Async);
\r
296 public static LogicalMethodInfo[] Create (MethodInfo[] method_infos, LogicalMethodTypes types)
\r
298 ArrayList sync = ((types & LogicalMethodTypes.Sync) != 0) ? new ArrayList () : null;
\r
299 ArrayList begin, end;
\r
301 if ((types & LogicalMethodTypes.Async) != 0){
\r
302 begin = new ArrayList ();
\r
303 end = new ArrayList ();
\r
305 begin = end = null;
\r
307 foreach (MethodInfo mi in method_infos){
\r
308 if (IsBeginMethod (mi) && begin != null)
\r
310 else if (IsEndMethod (mi) && end != null)
\r
312 else if (sync != null)
\r
316 int bcount = 0, count = 0;
\r
317 if (begin != null){
\r
318 bcount = count = begin.Count;
\r
319 if (count != end.Count)
\r
320 throw new InvalidOperationException ("Imbalance of begin/end methods");
\r
323 count += sync.Count;
\r
325 LogicalMethodInfo [] res = new LogicalMethodInfo [count];
\r
327 if (begin != null){
\r
328 foreach (MethodInfo bm in begin){
\r
329 string end_name = "End" + bm.Name.Substring (5);
\r
331 for (int i = 0; i < bcount; i++){
\r
332 MethodInfo em = (MethodInfo) end [i];
\r
333 if (em.Name == end_name){
\r
334 res [dest++] = new LogicalMethodInfo (bm, em);
\r
337 throw new InvalidOperationException ("Imbalance of begin/end methods");
\r
344 foreach (MethodInfo mi in sync){
\r
345 res [dest++] = new LogicalMethodInfo (mi);
\r
351 public object[] EndInvoke (object target, IAsyncResult asyncResult)
\r
353 if (parameters == null)
\r
354 ComputeParameters ();
\r
356 object[] values = new object [out_parameters.Length + 1];
\r
357 values [values.Length - 1] = asyncResult;
\r
358 object res = end_method_info.Invoke (target, values);
\r
360 int retc = IsVoid ? 0 : 1;
\r
361 object [] ret = new object [retc + out_parameters.Length];
\r
363 if (retc == 1) ret [0] = res;
\r
365 Array.Copy (values, 0, ret, retc, out_parameters.Length);
\r
369 public object GetCustomAttribute (Type type)
\r
371 return Attribute.GetCustomAttribute (method_info, type, false);
\r
374 public object[] GetCustomAttributes (Type type)
\r
376 return method_info.GetCustomAttributes (type, false);
\r
379 public object[] Invoke (object target, object[] values)
\r
381 if (parameters == null)
\r
382 ComputeParameters ();
\r
384 int retc = IsVoid ? 0 : 1;
\r
385 object [] ret = new object [retc + out_parameters.Length];
\r
386 object res = method_info.Invoke (target, values);
\r
387 if (retc == 1) ret [0] = res;
\r
390 for (int i = 0; i < parameters.Length; i++){
\r
391 if (parameters [i].ParameterType.IsByRef)
\r
392 ret [j++] = values [i];
\r
398 public static bool IsBeginMethod (MethodInfo method_info)
\r
400 if (method_info == null)
\r
401 throw new ArgumentNullException ("method_info can not be null");
\r
403 if (method_info.ReturnType != typeof (IAsyncResult))
\r
406 if (method_info.Name.StartsWith ("Begin"))
\r
412 public static bool IsEndMethod (MethodInfo method_info)
\r
414 if (method_info == null)
\r
415 throw new ArgumentNullException ("method_info can not be null");
\r
417 ParameterInfo [] parameter_info = method_info.GetParameters ();
\r
418 if (parameter_info.Length != 1)
\r
420 if (parameter_info [0].ParameterType != typeof (IAsyncResult))
\r
422 if (method_info.Name.StartsWith ("End"))
\r
428 public override string ToString ()
\r
430 StringBuilder sb = new StringBuilder ();
\r
431 if (parameters == null)
\r
432 ComputeParameters ();
\r
434 for (int i = 0; i < parameters.Length; i++){
\r
435 sb.Append (parameters [i].ParameterType);
\r
436 if (parameters [i].ParameterType.IsByRef)
\r
437 sb.Append (" ByRef");
\r
439 if (i+1 != parameters.Length)
\r
443 return String.Format (
\r
445 method_info.ReturnType, method_info.Name,
\r
449 #endregion // Methods
\r