aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/XmlRpcCS/XmlRpcExposedAttribute.cs
diff options
context:
space:
mode:
Diffstat (limited to 'XmlRpcCS/XmlRpcExposedAttribute.cs')
-rw-r--r--XmlRpcCS/XmlRpcExposedAttribute.cs60
1 files changed, 60 insertions, 0 deletions
diff --git a/XmlRpcCS/XmlRpcExposedAttribute.cs b/XmlRpcCS/XmlRpcExposedAttribute.cs
new file mode 100644
index 0000000..67b27ae
--- /dev/null
+++ b/XmlRpcCS/XmlRpcExposedAttribute.cs
@@ -0,0 +1,60 @@
1namespace Nwc.XmlRpc
2{
3 using System;
4 using System.Reflection;
5
6 /// <summary>
7 /// Simple tagging attribute to indicate participation is XML-RPC exposure.
8 /// </summary>
9 /// <remarks>
10 /// If present at the class level it indicates that this class does explicitly
11 /// expose methods. If present at the method level it denotes that the method
12 /// is exposed.
13 /// </remarks>
14 [AttributeUsage(
15 AttributeTargets.Class | AttributeTargets.Method,
16 AllowMultiple = false,
17 Inherited = true
18 )]
19 public class XmlRpcExposedAttribute : Attribute
20 {
21 /// <summary>Check if <paramref>obj</paramref> is an object utilizing the XML-RPC exposed Attribute.</summary>
22 /// <param name="obj"><c>Object</c> of a class or method to check for attribute.</param>
23 /// <returns><c>Boolean</c> true if attribute present.</returns>
24 public static Boolean ExposedObject(Object obj)
25 {
26 return IsExposed(obj.GetType());
27 }
28
29 /// <summary>Check if <paramref>obj</paramref>.<paramref>methodName</paramref> is an XML-RPC exposed method.</summary>
30 /// <remarks>A method is considered to be exposed if it exists and, either, the object does not use the XmlRpcExposed attribute,
31 /// or the object does use the XmlRpcExposed attribute and the method has the XmlRpcExposed attribute as well.</remarks>
32 /// <returns><c>Boolean</c> true if the method is exposed.</returns>
33 public static Boolean ExposedMethod(Object obj, String methodName)
34 {
35 Type type = obj.GetType();
36 MethodInfo method = type.GetMethod(methodName);
37
38 if (method == null)
39 throw new MissingMethodException("Method " + methodName + " not found.");
40
41 if (!IsExposed(type))
42 return true;
43
44 return IsExposed(method);
45 }
46
47 /// <summary>Check if <paramref>mi</paramref> is XML-RPC exposed.</summary>
48 /// <param name="mi"><c>MemberInfo</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 IsExposed(MemberInfo mi)
51 {
52 foreach (Attribute attr in mi.GetCustomAttributes(true))
53 {
54 if (attr is XmlRpcExposedAttribute)
55 return true;
56 }
57 return false;
58 }
59 }
60}