特性的定义——用于在运行时传递程序中各种元素(类,方法、结构、枚举、组件等)的行为信息的声明性标签。----特性不等于注释。
,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.WriteLine(userInfo.gender);
}
[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
{
///
/// 获取指定类型的注释
///
///
///
public static string GetDescription()
{
Type type = typeof(T);
string desp = "";
RemarkAttribute attr= type.GetCustomAttribute();
if (attr != null)
{
desp = attr.Description;
}
return desp;
}
///
/// 获取指定成员的注释
///
///
///
public static string GetProDescription(PropertyInfo prop)
{
string desp = "";
RemarkAttribute arrt = prop.GetCustomAttribute();//获取属性的自定义特性
if (arrt != null)
{
desp = arrt.Description;
}
return desp;
}
public static string GetMethodDescription(MethodInfo method)
{
string desp = "";
RemarkAttribute arrt = method.GetCustomAttribute();//获取属性的自定义特性
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());
//获取属性上的注释
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 { ///
/// 将字符串转换为Int类型 扩展方法:两个static 然后传入的变量前面要加this /// /// ///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
{
///
/// 获取指定类型对应的表名
///
///
///
public static string GetTableName(this Type type)
{
string tableName = "";
TableAttribute attr = type.GetCustomAttribute();
if (attr != null)
{
tableName = attr.TableName;
}
else
tableName = type.Name;
return tableName;
}
}
使用时——
//表名映射:
TableAttribute tableAttr = courseType.GetCustomAttribute();
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();//实例方法调用