通过反射访问属性(Attribute)信息的工具类
2009-11-15 18:50
369 查看
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
/// <summary>
/// 通过反射访问属性(Attribute)信息的工具类
/// </summary>
public static class AttributeHelper
{
/// <summary>
/// 获取某个类型包括指定属性的集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <returns></returns>
public static IList<T> GetCustomAttributes<T>(Type type) where T : Attribute
{
if (type == null) throw new ArgumentNullException("type");
T[] attributes = (T[])(type.GetCustomAttributes(typeof(T), false));
return (attributes.Length == 0) ? null : new List<T>(attributes);
}
/// <summary>
/// 获得某各类型包括指定属性的所有方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <returns></returns>
public static IList<MethodInfo> GetMethodsWithCustomAttribute<T>(Type type) where T : Attribute
{
if (type == null) throw new ArgumentNullException("type");
MethodInfo[] methods = type.GetMethods();
if ((methods == null) || (methods.Length == 0)) return null;
IList<MethodInfo> result = new List<MethodInfo>();
foreach (MethodInfo method in methods)
if (method.IsDefined(typeof(T), false))
result.Add(method);
return result.Count == 0 ? null : result;
}
/// <summary>
/// 获取某个方法指定类型属性的集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="method"></param>
/// <returns></returns>
public static IList<T> GetMethodCustomAttributes<T>(MethodInfo method) where T : Attribute
{
if (method == null) throw new ArgumentNullException("method");
T[] attributes = (T[])(method.GetCustomAttributes(typeof(T), false));
return (attributes.Length == 0) ? null : new List<T>(attributes);
}
/// <summary>
/// 获取某个方法指定类型的属性
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="method"></param>
/// <returns></returns>
public static T GetMethodCustomAttribute<T>(MethodInfo method) where T : Attribute
{
IList<T> attributes = GetMethodCustomAttributes<T>(method);
return (attributes == null) ? null : attributes[0];
}
/// <summary>
/// 获得某各类型包括指定属性的所有属性
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <returns></returns>
public static IList<PropertyInfo> GetPropertyWithCustomAttribute<T>(Type type) where T : Attribute
{
if (type == null) throw new ArgumentNullException("type");
PropertyInfo[] propes = type.GetProperties();
if ((propes == null) || (propes.Length == 0)) return null;
IList<PropertyInfo> result = new List<PropertyInfo>();
foreach (PropertyInfo p in propes)
if (p.IsDefined(typeof(T), false))
result.Add(p);
return result.Count == 0 ? null : result;
}
/// <summary>
/// 获取某个属性指定类型属性的集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="method"></param>
/// <returns></returns>
public static IList<T> GetPropertyCustomAttributes<T>(PropertyInfo property) where T : Attribute
{
if (property == null) throw new ArgumentNullException("method");
T[] attributes = (T[])(property.GetCustomAttributes(typeof(T), false));
return (attributes.Length == 0) ? null : new List<T>(attributes);
}
/// <summary>
/// 获取某个属性指定类型的属性
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="method"></param>
/// <returns></returns>
public static T GetPropertyCustomAttribute<T>(PropertyInfo property) where T : Attribute
{
IList<T> attributes = GetPropertyCustomAttributes<T>(property);
return (attributes == null) ? null : attributes[0];
}
}
using System.Collections.Generic;
using System.Text;
using System.Reflection;
/// <summary>
/// 通过反射访问属性(Attribute)信息的工具类
/// </summary>
public static class AttributeHelper
{
/// <summary>
/// 获取某个类型包括指定属性的集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <returns></returns>
public static IList<T> GetCustomAttributes<T>(Type type) where T : Attribute
{
if (type == null) throw new ArgumentNullException("type");
T[] attributes = (T[])(type.GetCustomAttributes(typeof(T), false));
return (attributes.Length == 0) ? null : new List<T>(attributes);
}
/// <summary>
/// 获得某各类型包括指定属性的所有方法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <returns></returns>
public static IList<MethodInfo> GetMethodsWithCustomAttribute<T>(Type type) where T : Attribute
{
if (type == null) throw new ArgumentNullException("type");
MethodInfo[] methods = type.GetMethods();
if ((methods == null) || (methods.Length == 0)) return null;
IList<MethodInfo> result = new List<MethodInfo>();
foreach (MethodInfo method in methods)
if (method.IsDefined(typeof(T), false))
result.Add(method);
return result.Count == 0 ? null : result;
}
/// <summary>
/// 获取某个方法指定类型属性的集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="method"></param>
/// <returns></returns>
public static IList<T> GetMethodCustomAttributes<T>(MethodInfo method) where T : Attribute
{
if (method == null) throw new ArgumentNullException("method");
T[] attributes = (T[])(method.GetCustomAttributes(typeof(T), false));
return (attributes.Length == 0) ? null : new List<T>(attributes);
}
/// <summary>
/// 获取某个方法指定类型的属性
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="method"></param>
/// <returns></returns>
public static T GetMethodCustomAttribute<T>(MethodInfo method) where T : Attribute
{
IList<T> attributes = GetMethodCustomAttributes<T>(method);
return (attributes == null) ? null : attributes[0];
}
/// <summary>
/// 获得某各类型包括指定属性的所有属性
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type"></param>
/// <returns></returns>
public static IList<PropertyInfo> GetPropertyWithCustomAttribute<T>(Type type) where T : Attribute
{
if (type == null) throw new ArgumentNullException("type");
PropertyInfo[] propes = type.GetProperties();
if ((propes == null) || (propes.Length == 0)) return null;
IList<PropertyInfo> result = new List<PropertyInfo>();
foreach (PropertyInfo p in propes)
if (p.IsDefined(typeof(T), false))
result.Add(p);
return result.Count == 0 ? null : result;
}
/// <summary>
/// 获取某个属性指定类型属性的集合
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="method"></param>
/// <returns></returns>
public static IList<T> GetPropertyCustomAttributes<T>(PropertyInfo property) where T : Attribute
{
if (property == null) throw new ArgumentNullException("method");
T[] attributes = (T[])(property.GetCustomAttributes(typeof(T), false));
return (attributes.Length == 0) ? null : new List<T>(attributes);
}
/// <summary>
/// 获取某个属性指定类型的属性
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="method"></param>
/// <returns></returns>
public static T GetPropertyCustomAttribute<T>(PropertyInfo property) where T : Attribute
{
IList<T> attributes = GetPropertyCustomAttributes<T>(property);
return (attributes == null) ? null : attributes[0];
}
}
相关文章推荐
- 通过java反射技术访问类中私有成员属性
- Java中通过反射获得对象的属性信息
- 常用工具类——应用程序属性信息访问类
- 3.C#中通过反射来获取Attribute中的信息
- 通过反射获取街道这个类的所有属性、所有方法信息
- 反射(2)—通过反射获取属性方法等信息
- 反射工具类访问方法和属性
- 通过反射得到绑定在类的属性上的特性信息
- 笔记:javascript通过反射和遍历,访问对象的所有属性,及 按引用/按值传递
- 通过反射访问私有属性并对其进行修改
- 通过反射访问任意类的私有方法和属性
- 通过反射获取构造方法,属性,方法信息
- 通过OpenSSL获取证书扩展属性之四:“CRL 分发点”和"颁发机构信息访问"
- Java中通过反射获得对象的属性信息
- [C#] 常用工具类——应用程序属性信息访问类
- C#中通过Type类访问数据类型信息
- PropertyInfo 类: 通过将对象反射,从而获取此对象所有属性,及对应的值
- 不使用反射进行C#属性的运行时动态访问
- java-反射(1)-通过反射改变对象的属性
- C#中的特性 和 通过反射获取属性上的特性