| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451 |
- //
- // System.Web.Services.Protocols.LogicalMethodInfo.cs
- //
- // Authors:
- // Miguel de Icaza ([email protected])
- // Tim Coleman ([email protected])
- // Lluis Sanchez Gual ([email protected])
- //
- // Copyright (C) Tim Coleman, 2002
- // Copyright (C) Ximian, Inc, 2003
- //
- // TODO:
- // BeginInvoke, EndInvoke are missing.
- // AsyncResultParameter
- //
- // WILD GUESS:
- // The reason for this class is so that it can cluster method/begin/end methods
- // together, as the begin/end methods in generated files from WSDL does *NOT*
- // contain all the information required to make a request.
- //
- // Either that, or the Begin*/End* versions probe the attributes on the regular
- // method (which seems simpler).
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System.Reflection;
- using System.Collections;
- using System.Text;
- using System.Web.Services;
- namespace System.Web.Services.Protocols {
- public sealed class LogicalMethodInfo {
- #region Fields
- MethodInfo method_info, end_method_info;
- ParameterInfo [] parameters;
- ParameterInfo [] out_parameters;
- ParameterInfo [] in_parameters;
- WebMethodAttribute attribute;
- #endregion // Fields.
-
- #region Constructors
-
- public LogicalMethodInfo (MethodInfo method_info)
- {
- if (method_info == null)
- throw new ArgumentNullException ("method_info should be non-null");
- if (method_info.IsStatic)
- throw new InvalidOperationException ("method is static");
-
- this.method_info = method_info;
- }
- //
- // Only an internal contructor, called from "Create"
- //
- LogicalMethodInfo (MethodInfo method_info, MethodInfo end_method_info)
- {
- if (method_info == null)
- throw new ArgumentNullException ("method_info should be non-null");
- if (method_info.IsStatic)
- throw new InvalidOperationException ("method is static");
-
- this.end_method_info = end_method_info;
- }
-
- #endregion // Constructors
- #region Properties
- //
- // Signatures for Begin/End methods:
- //
- // public System.IAsyncResult BeginHelloWorld(ARG1, ARG2, System.AsyncCallback callback, object asyncState) {
- // public string EndHelloWorld(System.IAsyncResult asyncResult) {
- public ParameterInfo AsyncCallbackParameter {
- get {
- ParameterInfo [] pi = method_info.GetParameters ();
- return pi [pi.Length-2];
- }
- }
- public ParameterInfo AsyncResultParameter {
- get {
- ParameterInfo [] pi = end_method_info.GetParameters ();
- return pi [pi.Length-1];
- }
- }
- public ParameterInfo AsyncStateParameter {
- get {
- ParameterInfo [] pi = method_info.GetParameters ();
- return pi [pi.Length-1];
- }
- }
- public MethodInfo BeginMethodInfo {
- get {
- if (IsBeginMethod (method_info))
- return method_info;
- return null;
- }
- }
- public ICustomAttributeProvider CustomAttributeProvider {
- get {
- return method_info;
- }
- }
- public Type DeclaringType {
- get {
- return method_info.DeclaringType;
- }
- }
- public MethodInfo EndMethodInfo {
- get {
- return end_method_info;
- }
- }
- public ParameterInfo[] InParameters {
- get {
- if (parameters == null)
- ComputeParameters ();
- return in_parameters;
- }
- }
- public bool IsAsync {
- get {
- return end_method_info != null;
- }
- }
- public bool IsVoid {
- get {
- return ReturnType == typeof (void);
- }
- }
- public MethodInfo MethodInfo {
- get {
- return method_info;
- }
- }
- public string Name {
- get {
- return method_info.Name;
- }
- }
- void ComputeParameters ()
- {
- ParameterInfo[] pars = method_info.GetParameters ();
- if (IsAsync)
- {
- parameters = new ParameterInfo [pars.Length - 2];
- Array.Copy (pars, 0, parameters, 0, pars.Length - 2);
- in_parameters = new ParameterInfo [parameters.Length];
- parameters.CopyTo (in_parameters, 0);
-
- ParameterInfo[] outPars = end_method_info.GetParameters ();
- out_parameters = new ParameterInfo [outPars.Length - 1];
- Array.Copy (outPars, 0, out_parameters, 0, out_parameters.Length);
- }
- else
- {
- parameters = pars;
- int out_count = 0;
- int in_count = 0;
-
- foreach (ParameterInfo p in parameters){
- Type ptype = p.ParameterType;
- if (ptype.IsByRef){
- out_count++;
- if (!p.IsOut)
- in_count++;
- } else
- in_count++;
- }
- out_parameters = new ParameterInfo [out_count];
- int i = 0;
- for (int j = 0; j < parameters.Length; j++){
- if (parameters [j].ParameterType.IsByRef)
- out_parameters [i++] = parameters [j];
- }
- in_parameters = new ParameterInfo [in_count];
- i = 0;
- for (int j = 0; j < parameters.Length; j++){
- if (parameters [j].ParameterType.IsByRef){
- if (!parameters [j].IsOut)
- in_parameters [i++] = parameters [j];
- } else
- in_parameters [i++] = parameters [j];
- }
- }
- }
-
- public ParameterInfo[] OutParameters {
- get {
- if (parameters == null)
- ComputeParameters ();
- return out_parameters;
- }
- }
- public ParameterInfo[] Parameters {
- get {
- if (parameters == null)
- ComputeParameters ();
- return parameters;
- }
- }
- public Type ReturnType {
- get {
- if (IsAsync)
- return end_method_info.ReturnType;
- else
- return method_info.ReturnType;
- }
- }
- public ICustomAttributeProvider ReturnTypeCustomAttributeProvider {
- get {
- return method_info.ReturnTypeCustomAttributes;
- }
- }
- internal bool EnableSession {
- get {
- if (method_info == null)
- return false;
- if (attribute == null) {
- object [] o = method_info.GetCustomAttributes (false);
- foreach (Attribute att in o) {
- if (att is WebMethodAttribute) {
- attribute = (WebMethodAttribute) att;
- break;
- }
- }
- }
- return (attribute != null) ? attribute.EnableSession : false;
- }
- }
- #endregion // Properties
- #region Methods
- public IAsyncResult BeginInvoke (object target, object[] values, AsyncCallback callback, object asyncState)
- {
- int len = (values!=null) ? values.Length : 0;
- object[] pars = new object [len + 2];
-
- if (len > 0)
- values.CopyTo (pars, 0);
-
- pars [len] = callback;
- pars [len+1] = asyncState;
-
- return (IAsyncResult) method_info.Invoke (target, pars);
- }
- public static LogicalMethodInfo[] Create (MethodInfo[] method_infos)
- {
- return Create (method_infos, LogicalMethodTypes.Sync | LogicalMethodTypes.Async);
- }
- public static LogicalMethodInfo[] Create (MethodInfo[] method_infos, LogicalMethodTypes types)
- {
- ArrayList sync = ((types & LogicalMethodTypes.Sync) != 0) ? new ArrayList () : null;
- ArrayList begin, end;
- if ((types & LogicalMethodTypes.Async) != 0){
- begin = new ArrayList ();
- end = new ArrayList ();
- } else
- begin = end = null;
- foreach (MethodInfo mi in method_infos){
- if (IsBeginMethod (mi) && begin != null)
- begin.Add (mi);
- else if (IsEndMethod (mi) && end != null)
- end.Add (mi);
- else if (sync != null)
- sync.Add (mi);
- }
- int bcount = 0, count = 0;
- if (begin != null){
- bcount = count = begin.Count;
- if (count != end.Count)
- throw new InvalidOperationException ("Imbalance of begin/end methods");
- }
- if (sync != null)
- count += sync.Count;
- LogicalMethodInfo [] res = new LogicalMethodInfo [count];
- int dest = 0;
- if (begin != null){
- foreach (MethodInfo bm in begin){
- string end_name = "End" + bm.Name.Substring (5);
- for (int i = 0; i < bcount; i++){
- MethodInfo em = (MethodInfo) end [i];
- if (em.Name == end_name){
- res [dest++] = new LogicalMethodInfo (bm, em);
- break;
- }
- throw new InvalidOperationException ("Imbalance of begin/end methods");
- }
- }
- }
- if (sync != null)
- foreach (MethodInfo mi in sync){
- res [dest++] = new LogicalMethodInfo (mi);
- }
-
- return res;
- }
- public object[] EndInvoke (object target, IAsyncResult asyncResult)
- {
- if (parameters == null)
- ComputeParameters ();
- object[] values = new object [out_parameters.Length + 1];
- values [values.Length - 1] = asyncResult;
- object res = end_method_info.Invoke (target, values);
-
- int retc = IsVoid ? 0 : 1;
- object [] ret = new object [retc + out_parameters.Length];
-
- if (retc == 1) ret [0] = res;
-
- Array.Copy (values, 0, ret, retc, out_parameters.Length);
- return ret;
- }
- public object GetCustomAttribute (Type type)
- {
- return Attribute.GetCustomAttribute (method_info, type, true);
- }
- public object[] GetCustomAttributes (Type type)
- {
- return method_info.GetCustomAttributes (type, true);
- }
- public object[] Invoke (object target, object[] values)
- {
- if (parameters == null)
- ComputeParameters ();
- int retc = IsVoid ? 0 : 1;
- object [] ret = new object [retc + out_parameters.Length];
- object res = method_info.Invoke (target, values);
- if (retc == 1) ret [0] = res;
- int j = retc;
- for (int i = 0; i < parameters.Length; i++){
- if (parameters [i].ParameterType.IsByRef)
- ret [j++] = values [i];
- }
- return ret;
- }
- public static bool IsBeginMethod (MethodInfo method_info)
- {
- if (method_info == null)
- throw new ArgumentNullException ("method_info can not be null");
- if (method_info.ReturnType != typeof (IAsyncResult))
- return false;
- if (method_info.Name.StartsWith ("Begin"))
- return true;
- return false;
- }
- public static bool IsEndMethod (MethodInfo method_info)
- {
- if (method_info == null)
- throw new ArgumentNullException ("method_info can not be null");
- ParameterInfo [] parameter_info = method_info.GetParameters ();
- if (parameter_info.Length != 1)
- return false;
- if (parameter_info [0].ParameterType != typeof (IAsyncResult))
- return false;
- if (method_info.Name.StartsWith ("End"))
- return true;
- return false;
- }
- public override string ToString ()
- {
- StringBuilder sb = new StringBuilder ();
- if (parameters == null)
- ComputeParameters ();
-
- for (int i = 0; i < parameters.Length; i++){
- sb.Append (parameters [i].ParameterType);
- if (parameters [i].ParameterType.IsByRef)
- sb.Append (" ByRef");
-
- if (i+1 != parameters.Length)
- sb.Append (", ");
- }
-
- return String.Format (
- "{0} {1} ({2})",
- method_info.ReturnType, method_info.Name,
- sb.ToString ());
- }
- #endregion // Methods
- }
- }
|