(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / LogicalMethodInfo.cs
blobb32f054f8d3512b135d8c441546823c9e57efcd9
1 //
2 // System.Web.Services.Protocols.LogicalMethodInfo.cs
3 //
4 // Authors:
5 // Miguel de Icaza (miguel@ximian.com)
6 // Tim Coleman (tim@timcoleman.com)
7 // Lluis Sanchez Gual (lluis@ximian.com)
8 //
9 // Copyright (C) Tim Coleman, 2002
10 // Copyright (C) Ximian, Inc, 2003
12 // TODO:
13 // BeginInvoke, EndInvoke are missing.
14 // AsyncResultParameter
16 // WILD GUESS:
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:
33 //
34 // The above copyright notice and this permission notice shall be
35 // included in all copies or substantial portions of the Software.
36 //
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;
48 using System.Text;
49 using System.Web.Services;
51 namespace System.Web.Services.Protocols {
52 public sealed class LogicalMethodInfo {
53 #region Fields
55 MethodInfo method_info, end_method_info;
56 ParameterInfo [] parameters;
57 ParameterInfo [] out_parameters;
58 ParameterInfo [] in_parameters;
59 WebMethodAttribute attribute;
61 #endregion // Fields.
63 #region Constructors
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
90 #region Properties
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 {
99 get {
100 ParameterInfo [] pi = method_info.GetParameters ();
101 return pi [pi.Length-2];
105 public ParameterInfo AsyncResultParameter {
106 get {
107 ParameterInfo [] pi = end_method_info.GetParameters ();
108 return pi [pi.Length-1];
112 public ParameterInfo AsyncStateParameter {
113 get {
114 ParameterInfo [] pi = method_info.GetParameters ();
115 return pi [pi.Length-1];
119 public MethodInfo BeginMethodInfo {
120 get {
121 if (IsBeginMethod (method_info))
122 return method_info;
123 return null;
127 public ICustomAttributeProvider CustomAttributeProvider {
128 get {
129 return method_info;
133 public Type DeclaringType {
134 get {
135 return method_info.DeclaringType;
139 public MethodInfo EndMethodInfo {
140 get {
141 return end_method_info;
145 public ParameterInfo[] InParameters {
146 get {
147 if (parameters == null)
148 ComputeParameters ();
149 return in_parameters;
153 public bool IsAsync {
154 get {
155 return end_method_info != null;
159 public bool IsVoid {
160 get {
161 return ReturnType == typeof (void);
165 public MethodInfo MethodInfo {
166 get {
167 return method_info;
171 public string Name {
172 get {
173 return method_info.Name;
177 void ComputeParameters ()
179 ParameterInfo[] pars = method_info.GetParameters ();
180 if (IsAsync)
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);
191 else
193 parameters = pars;
194 int out_count = 0;
195 int in_count = 0;
197 foreach (ParameterInfo p in parameters){
198 Type ptype = p.ParameterType;
199 if (ptype.IsByRef){
200 out_count++;
201 if (!p.IsOut)
202 in_count++;
203 } else
204 in_count++;
206 out_parameters = new ParameterInfo [out_count];
207 int i = 0;
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];
213 i = 0;
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];
218 } else
219 in_parameters [i++] = parameters [j];
224 public ParameterInfo[] OutParameters {
225 get {
226 if (parameters == null)
227 ComputeParameters ();
228 return out_parameters;
232 public ParameterInfo[] Parameters {
233 get {
234 if (parameters == null)
235 ComputeParameters ();
236 return parameters;
240 public Type ReturnType {
241 get {
242 if (IsAsync)
243 return end_method_info.ReturnType;
244 else
245 return method_info.ReturnType;
249 public ICustomAttributeProvider ReturnTypeCustomAttributeProvider {
250 get {
251 return method_info.ReturnTypeCustomAttributes;
255 internal bool EnableSession {
256 get {
257 if (method_info == null)
258 return false;
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;
265 break;
270 return (attribute != null) ? attribute.EnableSession : false;
273 #endregion // Properties
275 #region Methods
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];
282 if (len > 0)
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 ();
304 } else
305 begin = end = null;
307 foreach (MethodInfo mi in method_infos){
308 if (IsBeginMethod (mi) && begin != null)
309 begin.Add (mi);
310 else if (IsEndMethod (mi) && end != null)
311 end.Add (mi);
312 else if (sync != null)
313 sync.Add (mi);
316 int bcount = 0, count = 0;
317 if (begin != null){
318 bcount = count = begin.Count;
319 if (count != end.Count)
320 throw new InvalidOperationException ("Imbalance of begin/end methods");
322 if (sync != null)
323 count += sync.Count;
325 LogicalMethodInfo [] res = new LogicalMethodInfo [count];
326 int dest = 0;
327 if (begin != null){
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);
335 break;
337 throw new InvalidOperationException ("Imbalance of begin/end methods");
343 if (sync != null)
344 foreach (MethodInfo mi in sync){
345 res [dest++] = new LogicalMethodInfo (mi);
348 return res;
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);
366 return ret;
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;
389 int j = retc;
390 for (int i = 0; i < parameters.Length; i++){
391 if (parameters [i].ParameterType.IsByRef)
392 ret [j++] = values [i];
395 return ret;
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))
404 return false;
406 if (method_info.Name.StartsWith ("Begin"))
407 return true;
409 return false;
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)
419 return false;
420 if (parameter_info [0].ParameterType != typeof (IAsyncResult))
421 return false;
422 if (method_info.Name.StartsWith ("End"))
423 return true;
425 return false;
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)
440 sb.Append (", ");
443 return String.Format (
444 "{0} {1} ({2})",
445 method_info.ReturnType, method_info.Name,
446 sb.ToString ());
449 #endregion // Methods