XmlRpcExposedAttribute.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. namespace Nwc.XmlRpc
  2. {
  3. using System;
  4. using System.Reflection;
  5. /// <summary>
  6. /// Simple tagging attribute to indicate participation is XML-RPC exposure.
  7. /// </summary>
  8. /// <remarks>
  9. /// If present at the class level it indicates that this class does explicitly
  10. /// expose methods. If present at the method level it denotes that the method
  11. /// is exposed.
  12. /// </remarks>
  13. [AttributeUsage(
  14. AttributeTargets.Class | AttributeTargets.Method,
  15. AllowMultiple = false,
  16. Inherited = true
  17. )]
  18. public class XmlRpcExposedAttribute : Attribute
  19. {
  20. /// <summary>Check if <paramref>obj</paramref> is an object utilizing the XML-RPC exposed Attribute.</summary>
  21. /// <param name="obj"><c>Object</c> of a class or method to check for attribute.</param>
  22. /// <returns><c>Boolean</c> true if attribute present.</returns>
  23. public static Boolean ExposedObject(Object obj)
  24. {
  25. return IsExposed(obj.GetType());
  26. }
  27. /// <summary>Check if <paramref>obj</paramref>.<paramref>methodName</paramref> is an XML-RPC exposed method.</summary>
  28. /// <remarks>A method is considered to be exposed if it exists and, either, the object does not use the XmlRpcExposed attribute,
  29. /// or the object does use the XmlRpcExposed attribute and the method has the XmlRpcExposed attribute as well.</remarks>
  30. /// <returns><c>Boolean</c> true if the method is exposed.</returns>
  31. public static Boolean ExposedMethod(Object obj, String methodName)
  32. {
  33. Type type = obj.GetType();
  34. MethodInfo method = type.GetMethod(methodName);
  35. if (method == null)
  36. throw new MissingMethodException("Method " + methodName + " not found.");
  37. if (!IsExposed(type))
  38. return true;
  39. return IsExposed(method);
  40. }
  41. /// <summary>Check if <paramref>mi</paramref> is XML-RPC exposed.</summary>
  42. /// <param name="mi"><c>MemberInfo</c> of a class or method to check for attribute.</param>
  43. /// <returns><c>Boolean</c> true if attribute present.</returns>
  44. public static Boolean IsExposed(MemberInfo mi)
  45. {
  46. foreach (Attribute attr in mi.GetCustomAttributes(true))
  47. {
  48. if (attr is XmlRpcExposedAttribute)
  49. return true;
  50. }
  51. return false;
  52. }
  53. }
  54. }