XmlRpcExposedAttribute.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. namespace Nwc.XmlRpc
  29. {
  30. using System;
  31. using System.Reflection;
  32. /// <summary>
  33. /// Simple tagging attribute to indicate participation is XML-RPC exposure.
  34. /// </summary>
  35. /// <remarks>
  36. /// If present at the class level it indicates that this class does explicitly
  37. /// expose methods. If present at the method level it denotes that the method
  38. /// is exposed.
  39. /// </remarks>
  40. [AttributeUsage(
  41. AttributeTargets.Class | AttributeTargets.Method,
  42. AllowMultiple = false,
  43. Inherited = true
  44. )]
  45. public class XmlRpcExposedAttribute : Attribute
  46. {
  47. /// <summary>Check if <paramref>obj</paramref> is an object utilizing the XML-RPC exposed Attribute.</summary>
  48. /// <param name="obj"><c>Object</c> of a class or method to check for attribute.</param>
  49. /// <returns><c>Boolean</c> true if attribute present.</returns>
  50. public static Boolean ExposedObject(Object obj)
  51. {
  52. return IsExposed(obj.GetType());
  53. }
  54. /// <summary>Check if <paramref>obj</paramref>.<paramref>methodName</paramref> is an XML-RPC exposed method.</summary>
  55. /// <remarks>A method is considered to be exposed if it exists and, either, the object does not use the XmlRpcExposed attribute,
  56. /// or the object does use the XmlRpcExposed attribute and the method has the XmlRpcExposed attribute as well.</remarks>
  57. /// <returns><c>Boolean</c> true if the method is exposed.</returns>
  58. public static Boolean ExposedMethod(Object obj, String methodName)
  59. {
  60. Type type = obj.GetType();
  61. MethodInfo method = type.GetMethod(methodName);
  62. if (method == null)
  63. throw new MissingMethodException("Method " + methodName + " not found.");
  64. if (!IsExposed(type))
  65. return true;
  66. return IsExposed(method);
  67. }
  68. /// <summary>Check if <paramref>mi</paramref> is XML-RPC exposed.</summary>
  69. /// <param name="mi"><c>MemberInfo</c> of a class or method to check for attribute.</param>
  70. /// <returns><c>Boolean</c> true if attribute present.</returns>
  71. public static Boolean IsExposed(MemberInfo mi)
  72. {
  73. foreach (Attribute attr in mi.GetCustomAttributes(true))
  74. {
  75. if (attr is XmlRpcExposedAttribute)
  76. return true;
  77. }
  78. return false;
  79. }
  80. }
  81. }