特性:记忆是一个类,继承自Attribute,如果是的话,它就是特性
特性的继承
using System.Runtime.InteropServices;
namespace System.Diagnostics
{
//
// 摘要:
// 指示调试器逐句通过代码,而不是单步执行代码。 无法继承此类。
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Constructor | AttributeTargets.Method, Inherited = false)]
[ComVisible(true)]
public sealed class DebuggerStepThroughAttribute : Attribute
{
//
// 摘要:
// 初始化 System.Diagnostics.DebuggerStepThroughAttribute 类的新实例。
public DebuggerStepThroughAttribute();
}
}
示例如下:
using System;
using System.Diagnostics;
namespace AdvancedAttribute
{
class Program
{
static void Main(string[] args)
{
UseAttributeClass useAttributeClass = new UseAttributeClass();
useAttributeClass.DoMethod();
Console.ReadKey();
}
}
public class UseAttributeClass
{
//举例适应的特性[DebuggerStepThrough]
[DebuggerStepThrough]
public void DoMethod()
{
Console.WriteLine("这个方法很简单");
}
}
}
特性使用场景:可以用来做数据验证
public class ResetPasswordViewModel
{
[Required]
[EmailAddress]
[Display(Name = "电子邮件")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "{0} 必须至少包含 {2} 个字符。", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "密码")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "确认密码")]
[Compare("Password", ErrorMessage = "密码和确认密码不匹配。")]
public string ConfirmPassword { get; set; }
public string Code { get; set; }
}
public class ForgotPasswordViewModel
{
[Required]
[EmailAddress]
[Display(Name = "电子邮件")]
public string Email { get; set; }
}
目标:学会后要做一个O/RM框架
[Authorize]
public class AccountController : Controller
{
private ApplicationSignInManager _signInManager;
private ApplicationUserManager _userManager;
public AccountController()
{
}
}
使用场景
类 Attribute 将预定义的系统信息或用户定义的自定义信息与目标元素相关联。 目标元素可以是程序集、类、构造函数、委托、枚举、事件、字段、接口、方法、可移植可执行文件模块、参数、属性、返回值、结构或其他属性。
属性提供的信息也称为元数据。 应用程序可以在运行时检查元数据,以控制程序处理数据的方式,或在运行时由外部工具控制应用程序本身的处理和维护方式之前。 例如,.NET 预定义并使用属性类型来控制运行时行为,某些编程语言使用属性类型来表示 .NET 通用类型系统不直接支持的语言功能。
所有属性类型都直接或间接派生自 Attribute 类。 属性可以应用于任何目标元素;多个属性可以应用于同一目标元素;和属性可由派生自目标元素的元素继承。 使用 AttributeTargets 类指定应用属性的目标元素。
目前哪些地方使用到了特性:几乎所有的框架都用到了 MVC-- WebApi --EF–IOC–AOP
特性分类:
一,系统自带特性 DebuggerStepThrough,Obsolete等等有一些是影响到了编译器的运行
二,自定义
特性的创建 特性:贴标签–贴上标签就产生了新的功能
特性看成类
using System;
namespace AdvancedAttribute1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("---------------------创建特性----------------------");
AttributeTest attributeTest = new AttributeTest();
attributeTest.Test();
Console.ReadKey();
}
}
#region 创建特性
class AttributeTest
{
public void Test()
{
Type type = typeof(UseAttribute);
object[] customAttributes = type.GetCustomAttributes(true);
foreach (object customAttribute in customAttributes)
{
DefindAttribute defindAttribute = customAttribute as DefindAttribute;
if (defindAttribute != null)
{
Console.WriteLine(defindAttribute.ShowInfo);
}
}
}
}
[Defind("这是第一个特性的创建!")]
class UseAttribute
{
}
class DefindAttribute : Attribute
{
public DefindAttribute(string showInfo)
{
ShowInfo = showInfo;
}
public string ShowInfo { get; set; }
}
#endregion
}