C#特性的介绍及应用场景
2024-09-30 13:46:54
1.特性的任务:特性就是为了支持对象添加一些自我描述的信息,不影响类封装的前提添加额外信息。如果你用这个信息,那特性就有用;如果你不需要这个信息,那么这个特性就没用。
2.特性的基类:Attribute。例如:Obsolete特性,提出警告信息或错误信息,特性可以影响编译、影响运行。
3.特性类通常用Attribute结尾,在使用的时候可以用全称,也可以去掉这个结尾,也可以加上小括号显示调用构造函数,如果不加小括号默认调用无参构造函数,也可以在括号内直接给属性或字段赋值。
4.特性往往只能修饰一个对象一次,需要设置属性的属性的时候,需要给属性添加AttributeUsage属性,可以用来设置:是否允许多次修饰、修饰对象的类别(类or字段等)
5.DLL文件=IL中间语言+metadata元数据,特性信息会被编译到元数据中。
6.使用场景
6.1为类或成员添加描述信息,然后在使用的时候拿到该信息。
/// <summary>
/// 描述实体映射的数据表
/// </summary>
public class TableNameAttribute : Attribute
{
public TableNameAttribute()
{ }
public string TableName { get; set; }
} [TableName(TableName = "Users")]
public class User
{
public int Id { get; set; }
public string Name { get; set; }
} class Program
{
public static void Main(string[] args)
{
User user = new User();
Type t = user.GetType();
object[] customAttrs = t.GetCustomAttributes(true);
for (int i = ; i < customAttrs.Length; i++)
{
if (customAttrs[i] is TableNameAttribute)
{
TableNameAttribute tnAttr = (TableNameAttribute)customAttrs[i];
Console.WriteLine(tnAttr.TableName);
}
}
}
}
6.2 做数据验证
public class IntValidateAttribute : Attribute
{
/// <summary>
/// 最小值
/// </summary>
private int minValue { get; set; }
/// <summary>
/// 最大值
/// </summary>
private int maxValue { get; set; }
/// <summary>
/// 构造函数
/// </summary>
/// <param name="minValue"></param>
/// <param name="maxValue"></param>
public IntValidateAttribute(int minValue, int maxValue)
{
this.minValue = minValue;
this.maxValue = maxValue;
}
/// <summary>
/// 检验值是否合法
/// </summary>
/// <param name="checkValue"></param>
/// <returns></returns>
public bool Validate(int checkValue)
{
return checkValue >= minValue && checkValue <= maxValue;
}
} public class User
{
[IntValidate(, )]
public int Id { get; set; }
public string Name { get; set; }
} public class BaseDal
{
public static string Insert<T>(T model)
{
Type modelType = typeof(T);
//获取类型的所有属性
PropertyInfo[] propertyInfos = modelType.GetProperties(); bool boIsCheck = true;
//循环所有属性
foreach (var property in propertyInfos)
{
//获取属性的所有特性
object[] attrs = property.GetCustomAttributes(true);
if (property.PropertyType.Name.ToLower().Contains("int"))
{
foreach (var attr in attrs)
{
if (attr is IntValidateAttribute)
{
IntValidateAttribute intValidate = (IntValidateAttribute)attr;
//执行特性的验证逻辑
boIsCheck = intValidate.Validate((int)property.GetValue(model));
}
}
}
if (!boIsCheck)
{
break;
}
}
if (boIsCheck)
{
return "验证通过,插入数据库成功";
}
else
{
return "验证失败";
}
}
}
class Program
{
public static void Main(string[] args)
{
string msg = BaseDal.Insert<User>(new User() { Id = , Name = "lvcc" });
Console.WriteLine(msg);
}
}
6.3 添加说明信息并获取,同6.1
/// <summary>
/// 备注特性
/// </summary>
public class RemarkAttribute : Attribute
{
private string Remark { get; set; }
public RemarkAttribute(string Remark)
{
this.Remark = Remark;
}
public string GetRemark()
{
return this.Remark;
}
} public enum ESex
{
[Attributes.Remark("男的")]
boy=
,
[Attributes.Remark("女的")]
girl =
} /// <summary>
/// 提供扩展方法
/// </summary>
public static class EnumExtension
{
public static string GetRemark(this Enum model)
{
if (model is ESex)
{
Type type = typeof(ESex);
FieldInfo fi = type.GetField(model.ToString()); object[] attributes = fi.GetCustomAttributes(true);
foreach (var attr in attributes)
{
if (attr is RemarkAttribute)
{
RemarkAttribute remark = (RemarkAttribute)attr;
return remark.GetRemark();
}
}
} return string.Empty;
}
} class Program
{
public static void Main(string[] args)
{
Console.WriteLine(ESex.boy.GetRemark());
}
}
最新文章
- MIS系统开发利器,快速的字典录入解决方案,另类的、可管理的.NET DataWindow
- Cassandra中的数据一致性
- 【bzoj1066】[SCOI2007]蜥蜴 网络最大流
- Oracle Flashback Technologies (总)
- iOS -不同模拟器字体适配
- Redis哈希相关命令
- 终极解决方案 at org.apache.jsp.index_jsp._jspInit(index_jsp.java:22) 报空指针
- HDU 2037 今年暑假不AC(贪心,区间更新,板子题)
- thinkphp5自动完成
- BSA Network Shell系列-通过NSH执行Powershell,VBScript或bat files脚本
- GDAL创建图像提示Driver xxx does not support XXX creation option的原因
- android 解析服务器数据使用json还是xml方式
- Javapoet源码解析
- udp/tcp流程
- cocos2dx 3.17.1 导演类
- dubbo服务达成jar包
- 说说PHP中foreach引用的一个坑
- mysql 约束条件目录
- String比较相等的问题探索
- HDU.2147 kiki's game (博弈论 PN分析)