0
点赞
收藏
分享

微信扫一扫

自定义特性Attribute实现

慎壹 2022-11-18 阅读 148
编程语言


一定义    

/// <summary>

    /// 声明需要认证的特性


    /// by 贾世义 2015-11-18


    /// </summary>


    public class NeedPassAttribute:Attribute


    {


        /// <summary>


        /// 邮箱认证状态


        /// </summary>


        public bool PassEmail { get; set; }

   }

二声明

在需要的声明此特性的Action上声明

[NeedPassAttribute(PassEmail =true)]

三使用

在Controler的基类中

 var attrNeeds = filterContext.ActionDescriptor.GetCustomAttributes(typeof(NeedPassAttribute), false);
                    if (attrNeeds.Length == 1)
                    {
                        NeedPassAttribute needPass = attrNeeds[0] as NeedPassAttribute;

if (needPass.PassEmail){ 你的执行代码}



举报

相关推荐

0 条评论