特性的定义——用于在运行时传递程序中各种元素(类,方法、结构、枚举、组件等)的行为信息的声明性标签。----特性不等于注释。
,net 有两种类型特性——预定义特性和自定义特性
创建的自定义特性的命名方式——自定义特性+Attribute。该类继承于Attribute
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace WinformControl.CustAttrs
{//指定特性应用于哪些元素[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Method)]public class RemarkAttribute : Attribute{public string Description { get; set; }public RemarkAttribute(string desp){Description = desp;}}
}
然后在编写类时如下进行自定义特性的添加——
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WinformControl.CustAttrs;namespace WinformControl
{//自定义特性[Remark("UserInfos")]public class UserInfo{//[Remark("空参构造函数")],只对类,属性,方法有效public UserInfo(){Console.WriteLine("空参构造函数方法");}public UserInfo(int aga,string name){Console.WriteLine(aga.ToString()+name);}[Remark("用户ID")]public int UserID{get;set;}[Remark("用户年龄")]public int UserAge{get;set;}[Remark("用户名字")]public string UserName{get;set;}private string gender = "female";public string Gender{get;set;}[Remark("无参Show方法")]public void Show(){Console.WriteLine("你好");}[Remark("传参Show方法")]public void Show(UserInfo userInfo){Console.der);}[Remark("静态show方法")]public static void ShowInfo(){Console.WriteLine("调用静态方法");}}
}
编写一个获取类型注释的类——
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WinformControl.CustAttrs;namespace WinformControl
{public class RemarkHelper{/// <summary>/// 获取指定类型的注释/// </summary>/// <param name="type"></param>/// <returns></returns>public static string GetDescription<T>(){Type type = typeof(T);string desp = "";RemarkAttribute attr= type.GetCustomAttribute<RemarkAttribute>();if (attr != null){desp = attr.Description;}return desp;}/// <summary>/// 获取指定成员的注释/// </summary>/// <param name="pro"></param>/// <returns></returns>public static string GetProDescription(PropertyInfo prop){string desp = "";RemarkAttribute arrt = prop.GetCustomAttribute<RemarkAttribute>();//获取属性的自定义特性if (arrt != null){desp = arrt.Description;}return desp;}public static string GetMethodDescription(MethodInfo method){string desp = "";RemarkAttribute arrt = method.GetCustomAttribute<RemarkAttribute>();//获取属性的自定义特性if (arrt != null){desp = arrt.Description;}return desp;}}
}
使用——
#region 特性UserInfo user1 = new UserInfo();user1.UserID = 1;user1.UserName = "黎明";//类、属性、方法、注释文件拿到Type userType = typeof(UserInfo);//获取指定类型的特性Console.WriteLine($"CourseInfo的注释信息:" + RemarkHelper.GetDescription<UserInfo>());//获取属性上的注释var props = userType.GetProperties();foreach(var p in props){Console.WriteLine($"{p.Name} 的注释信息:" + RemarkHelper.GetProDescription(p));}//获取方法的特性var methods = userType.GetMethods();foreach (var m in methods){Console.WriteLine($"{m.Name} 的注释信息:" + RemarkHelper.GetMethodDescription(m));}#endregion
//1.声明自定义特性——新建一个自定义特性类
//2.构建特性——在我们想要应用的属性或者类,方法等上构建特性
//3.应用自定义特性——使用这些特性(主要是通过反射)
表名映射用到的就是就是扩展方法加特性来实现的。
新建一个表特性类——
[AttributeUsage(AttributeTargets.Class)]public class TableAttribute:Attribute{public string TableName { get; set; }public TableAttribute(string tableName){TableName = tableName;}}
然后我们需要理解什么是扩展方法——就是一个静态类中的静态方法
在调用时,能够直接在扩展方法指定的类型的变量后面直接 变量 .方法 就可以i调用,而不需要用类似与StringBuilder.GetInt(变量)来实现。
例如—
public static class StringHelper{/// <summary>/// 将字符串转换为Int类型 扩展方法:两个static 然后传入的变量前面要加this/// </summary>/// <param name="str"></param>/// <returns></returns>public static int GetInt(this string str){int reInt = 0;int.TryParse(str, out reInt);return reInt;}}
使用时——
有区别
string str = "1";int intVal = str.GetInt();//调用扩展方法int val1 = StringHelper.GetInt(str);
静态方法创建如下——
//扩展方法:静态类中静态方法public static class AttributeHelper{/// <summary>/// 获取指定类型对应的表名/// </summary>/// <param name="type"></param>/// <returns></returns>public static string GetTableName(this Type type){string tableName = "";TableAttribute attr = type.GetCustomAttribute<TableAttribute>();if (attr != null){tableName = attr.TableName;}elsetableName = type.Name;return tableName;}}
使用时——
//表名映射:TableAttribute tableAttr = courseType.GetCustomAttribute<TableAttribute>();string tableName = "";if (tableAttr!=null){tableName= tableAttr.TableName;}else{tableName = courseType.Name;}Console.WriteLine($"CourseInfo对应的表名是:{tableName}");//表名映射实现步骤://1.建特性:TableAttribute//2.应用特性,指定真实表名//3.封装扩展方法:GetTableName(this Type type)//4.指定类型的Type对象调用 GetTableName()即可获取到表名string tableName1 = courseType.GetTableName();course.ShowCourse();//实例方法调用
本文发布于:2024-02-02 16:19:37,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170686197344969.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |