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