using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace XFEExtension.AttributeExtension
{
///
/// 属性拓展类
///
public static class AttributeExtension
{
#region 类名方法
///
/// 获取给定类的某个方法的给定属性的对象
///
/// 属性类型
/// 类名(区分大小写)
/// 类中的方法名
///
public static T GetAttributeInMethod(string className, string methodName) where T : Attribute
{
var type = Assembly.GetCallingAssembly().GetType(className, false, true);
if (type is null)
{
throw new XFEExtensionException($"未找到类{className}");
}
else
{
var method = type.GetMethod(methodName);
if (method is null)
{
throw new XFEExtensionException($"未找到方法{methodName}");
}
else
{
return method.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
}
}
}
///
/// 获取给定类的某个方法的给定属性的对象列表
///
/// 属性类型
/// 类名(区分大小写)
/// 类中的方法名
///
public static List GetAttributesInMethod(string className, string methodName) where T : Attribute
{
var type = Assembly.GetCallingAssembly().GetType(className, false, true);
if (type is null)
{
throw new XFEExtensionException($"未找到类{className}");
}
else
{
var method = type.GetMethod(methodName);
if (method is null)
{
throw new XFEExtensionException($"未找到方法{methodName}");
}
else
{
return method.GetCustomAttributes(typeof(T), false).Select(x => x as T).ToList();
}
}
}
///
/// 获取给定类的某个字段的给定属性的对象
///
/// 属性类型
/// 类名(区分大小写)
/// 类中的字段名称
///
public static T GetAttributeInField(string className, string fieldName) where T : Attribute
{
var type = Assembly.GetCallingAssembly().GetType(className, false, true);
if (type is null)
{
throw new XFEExtensionException($"未找到类{className}");
}
else
{
var field = type.GetField(fieldName);
if (field is null)
{
throw new XFEExtensionException($"未找到字段{fieldName}");
}
else
{
return field.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
}
}
}
///
/// 获取给定类的某个字段的给定属性的对象列表
///
/// 属性类型
/// 类名(区分大小写)
/// 类中的字段名称
///
public static List GetAttributesInField(string className, string fieldName) where T : Attribute
{
var type = Assembly.GetCallingAssembly().GetType(className, false, true);
if (type is null)
{
throw new XFEExtensionException($"未找到类{className}");
}
else
{
var field = type.GetField(fieldName);
if (field is null)
{
throw new XFEExtensionException($"未找到字段{fieldName}");
}
else
{
return field.GetCustomAttributes(typeof(T), false).Select(x => x as T).ToList();
}
}
}
///
/// 获取给定类的某个属性的给定属性的对象
///
/// 属性类型
/// 类名(区分大小写)
/// 类中的属性名称
///
public static T GetAttributeInProperty(string className, string propertyName) where T : Attribute
{
var type = Assembly.GetCallingAssembly().GetType(className, false, true);
if (type is null)
{
throw new XFEExtensionException($"未找到类{className}");
}
else
{
var property = type.GetProperty(propertyName);
if (property is null)
{
throw new XFEExtensionException($"未找到属性{propertyName}");
}
else
{
return property.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
}
}
}
///
/// 获取给定类的某个属性的给定属性的对象列表
///
/// 属性类型
/// 类名(区分大小写)
/// 类中的属性名称
///
public static List GetAttributesInProperty(string className, string propertyName) where T : Attribute
{
var type = Assembly.GetCallingAssembly().GetType(className, false, true);
if (type is null)
{
throw new XFEExtensionException($"未找到类{className}");
}
else
{
var property = type.GetProperty(propertyName);
if (property is null)
{
throw new XFEExtensionException($"未找到属性{propertyName}");
}
else
{
return property.GetCustomAttributes(typeof(T), false).Select(x => x as T).ToList();
}
}
}
///
/// 获取给定类的给定方法的给定参数的给定属性的对象
///
/// 属性类型
/// 类名(区分大小写)
/// 方法名
/// 参数名
///
public static T GetAttributeInParam(string className, string methodName, string paramName) where T : Attribute
{
var type = Assembly.GetCallingAssembly().GetType(className, false, true);
if (type is null)
{
throw new XFEExtensionException($"未找到类{className}");
}
else
{
var method = type.GetMethod(methodName);
if (method is null)
{
throw new XFEExtensionException($"未找到方法{methodName}");
}
else
{
var param = method.GetParameters().FirstOrDefault(x => x.Name == paramName);
if (param is null)
{
throw new XFEExtensionException($"未找到参数{paramName}");
}
else
{
return param.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
}
}
}
}
///
/// 获取给定类的给定方法的给定参数的给定属性的对象列表
///
/// 属性类型
/// 类名(区分大小写)
/// 方法名
/// 参数名
///
public static List GetAttributesInParam(string className, string methodName, string paramName) where T : Attribute
{
var type = Assembly.GetCallingAssembly().GetType(className, false, true);
if (type is null)
{
throw new XFEExtensionException($"未找到类{className}");
}
else
{
var method = type.GetMethod(methodName);
if (method is null)
{
throw new XFEExtensionException($"未找到方法{methodName}");
}
else
{
var param = method.GetParameters().FirstOrDefault(x => x.Name == paramName);
if (param is null)
{
throw new XFEExtensionException($"未找到参数{paramName}");
}
else
{
return param.GetCustomAttributes(typeof(T), false).Select(x => x as T).ToList();
}
}
}
}
///
/// 给定类的给定属性的对象
///
///
///
///
public static T GetAttributeOnClass(string className) where T : Attribute
{
var type = Assembly.GetCallingAssembly().GetType(className, false, true);
if (type is null)
{
throw new XFEExtensionException($"未找到类{className}");
}
else
{
return type.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
}
}
///
/// 给定类的给定属性的对象列表
///
///
///
///
public static List GetAttributesOnClass(string className) where T : Attribute
{
var type = Assembly.GetCallingAssembly().GetType(className, false, true);
if (type is null)
{
throw new XFEExtensionException($"未找到类{className}");
}
else
{
return type.GetCustomAttributes(typeof(T), false).Select(x => x as T).ToList();
}
}
#endregion
#region 拓展方法
///
/// 获取该对象的给定属性的对象
///
/// 属性类型
/// 对象
public static T GetAttribute(this object obj) where T : Attribute
{
if (obj is string || obj is int || obj is double || obj is float || obj is decimal || obj is DateTime || obj is bool)
{
throw new XFEExtensionException("基础类型不支持获取属性");
}
return obj.GetType().GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
}
///
/// 获取该对象的给定属性的对象列表
///
/// 属性类型
/// 对象
public static List GetAttributes(this object obj) where T : Attribute
{
if (obj is string || obj is int || obj is double || obj is float || obj is decimal || obj is DateTime || obj is bool)
{
throw new XFEExtensionException("基础类型不支持获取属性");
}
return obj.GetType().GetCustomAttributes(typeof(T), false).Select(x => x as T).ToList(); ;
}
///
/// 获取给定类的某个方法的给定属性的对象
///
/// 属性类型
/// 类
/// 类中的方法名
///
public static T GetAttributeInMethod(this object tarClass, string methodName) where T : Attribute
{
var className = tarClass.GetType().ToString();
var type = Assembly.GetCallingAssembly().GetType(className, false, true) ?? throw new XFEExtensionException($"未找到类{className}");
var method = type.GetMethod(methodName);
if (method is null)
{
throw new XFEExtensionException($"未找到方法{methodName}");
}
else
{
return method.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
}
}
///
/// 获取给定类的某个方法的给定属性的对象列表
///
/// 属性类型
/// 类
/// 类中的方法名
///
public static List GetAttributesInMethod(this object tarClass, string methodName) where T : Attribute
{
var className = tarClass.GetType().ToString();
var type = Assembly.GetCallingAssembly().GetType(className, false, true) ?? throw new XFEExtensionException($"未找到类{className}");
var method = type.GetMethod(methodName);
if (method is null)
{
throw new XFEExtensionException($"未找到方法{methodName}");
}
else
{
return method.GetCustomAttributes(typeof(T), false).Select(x => x as T).ToList();
}
}
///
/// 获取给定类的某个字段的给定属性的对象
///
/// 属性类型
/// 类
/// 类中的字段名称
///
public static T GetAttributeInField(this object tarClass, string fieldName) where T : Attribute
{
var className = tarClass.GetType().ToString();
var type = Assembly.GetCallingAssembly().GetType(className, false, true) ?? throw new XFEExtensionException($"未找到类{className}");
var field = type.GetField(fieldName);
if (field is null)
{
throw new XFEExtensionException($"未找到字段{fieldName}");
}
else
{
return field.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
}
}
///
/// 获取给定类的某个字段的给定属性的对象列表
///
/// 属性类型
/// 类
/// 类中的字段名称
///
public static List GetAttributesInField(this object tarClass, string fieldName) where T : Attribute
{
var className = tarClass.GetType().ToString();
var type = Assembly.GetCallingAssembly().GetType(className, false, true) ?? throw new XFEExtensionException($"未找到类{className}");
var field = type.GetField(fieldName);
if (field is null)
{
throw new XFEExtensionException($"未找到字段{fieldName}");
}
else
{
return field.GetCustomAttributes(typeof(T), false).Select(x => x as T).ToList();
}
}
///
/// 获取给定类的某个属性的给定属性的对象
///
/// 属性类型
/// 类
/// 类中的属性名称
///
public static T GetAttributeInProperty(this object tarClass, string propertyName) where T : Attribute
{
var className = tarClass.GetType().ToString();
var type = Assembly.GetCallingAssembly().GetType(className, false, true) ?? throw new XFEExtensionException($"未找到类{className}");
var property = type.GetProperty(propertyName);
if (property is null)
{
throw new XFEExtensionException($"未找到属性{propertyName}");
}
else
{
return property.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
}
}
///
/// 获取给定类的某个属性的给定属性的对象列表
///
/// 属性类型
/// 类
/// 类中的属性名称
///
public static List GetAttributesInProperty(this object tarClass, string propertyName) where T : Attribute
{
var className = tarClass.GetType().ToString();
var type = Assembly.GetCallingAssembly().GetType(className, false, true) ?? throw new XFEExtensionException($"未找到类{className}");
var property = type.GetProperty(propertyName);
if (property is null)
{
throw new XFEExtensionException($"未找到属性{propertyName}");
}
else
{
return property.GetCustomAttributes(typeof(T), false).Select(x => x as T).ToList();
}
}
///
/// 获取给定类的给定方法的给定参数的给定属性的对象
///
/// 属性类型
/// 类
/// 方法名
/// 参数名
///
public static T GetAttributeInParam(this object tarClass, string methodName, string paramName) where T : Attribute
{
var className = tarClass.GetType().ToString();
var type = Assembly.GetCallingAssembly().GetType(className, false, true) ?? throw new XFEExtensionException($"未找到类{className}");
var method = type.GetMethod(methodName);
if (method is null)
{
throw new XFEExtensionException($"未找到方法{methodName}");
}
else
{
var param = method.GetParameters().FirstOrDefault(x => x.Name == paramName);
if (param is null)
{
throw new XFEExtensionException($"未找到参数{paramName}");
}
else
{
return param.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
}
}
}
///
/// 获取给定类的给定方法的给定参数的给定属性的对象列表
///
/// 属性类型
/// 类名(区分大小写)
/// 方法名
/// 参数名
///
public static List GetAttributesInParam(this object tarClass, string methodName, string paramName) where T : Attribute
{
var className = tarClass.GetType().ToString();
var type = Assembly.GetCallingAssembly().GetType(className, false, true) ?? throw new XFEExtensionException($"未找到类{className}");
var method = type.GetMethod(methodName);
if (method is null)
{
throw new XFEExtensionException($"未找到方法{methodName}");
}
else
{
var param = method.GetParameters().FirstOrDefault(x => x.Name == paramName);
if (param is null)
{
throw new XFEExtensionException($"未找到参数{paramName}");
}
else
{
return param.GetCustomAttributes(typeof(T), false).Select(x => x as T).ToList();
}
}
}
#endregion
}
}