zoukankan      html  css  js  c++  java
  • 浅谈Attribute [C# | Attribute | DefaultValueAttribute]

    前言

         最近做权限控制,对页面的权限控制使用IHttpModule做的,想把控制粒度细化到页面上控件的权限判断,意图是传入控件编号,根据控件编号和当前用户的权限,如果没有权限的话就把控件隐藏或显示为不可用,打算用Attribute来做,以下是遇到的一些问题和看法,虽然失败了但是觉得仍然有参考价值。

    推荐几篇文章:

              1.     Attribute在.net编程中的应用(一)

              2.     Attribute在.net编程中的应用(二)

              3.     Attribute在.NET编程中的应用(三)

              4.     Attribute在.NET编程中的应用(四)

              5.     CLR笔记:17.自定义属性

    正文:

              先看我的错误代码

            [AttributeUsage(AttributeTargets.Class | AttributeTargets.Field | AttributeTargets.Method)]
            
    public class PowerAttribute : Attribute
            {
                
    /// <summary>
                
    /// 对页面内控件进行权限判断,权限的字符串组成:URL+"?id"+Control.ID
                
    ///     例如:/page/index?id=tbName
                
    /// </summary>
                
    /// <param name="pType"></param>
                
    /// <param name="control"></param>
                public PowerAttribute(PowerType pType, params WebControl[] control)
                {
                    
    for (int i = control.Length; -->= 0; )
                    {
                        WebControl Control 
    = control.GetValue(i) as WebControl;
                        
    if (Control != null)
                            
    if (HasPower(string.Concat(WebHelper.WebRequest.Path, "?id=", Control.ID)) == -1)//判断权限
                                if (pType == PowerType.Visible)
                                    Control.Visible 
    = false;
                                
    else
                                    Control.Enabled 
    = false;
                    }
                }

                
    public int HasPower(string path)
                {
                    
    return -1;

                }

                
    /// <summary>
                
    /// 获得控件的权限,1 具有权限,0 用户超时,-1 没有权限
                
    /// </summary>
                
    /// <param name="control"></param>
                
    /// <param name="Field"></param>
                public PowerAttribute(WebControl control, ref int Field)
                {
                    Field 
    = HasPower(string.Concat(WebHelper.WebRequest.Path, "?id=", control.ID));
                }

                
    /// <summary>
                
    /// 获得页面的权限,1 具有权限,0 用户超时,-1 没有权限
                
    /// </summary>
                
    /// <param name="control"></param>
                
    /// <param name="Field"></param>
                public PowerAttribute(ref int Field)
                {
                    Field 
    = HasPower(WebHelper.WebRequest.Path);
                }
            }

              意图比较明显,想在要控制控件权限的页面里面加一个Attribute标记传入控件编号就能自动把其设置为指定的显示状态,可是失败了!以下是错误信息:

    属性参数必须是常量表达式、typeof 表达式或数组创建表达式 ConsoleTest

    也就是说没法传,也不能够用ref来传递引用!也就是说没有办法直接改变页面上的控件或字段的值,这让我忽然想起了DefaultValueAttribute,MSDN的解释是

    指定属性 (Property) 的默认值。我发现有许多朋友有点误会这个,先看代码:

            static void Main(string[] args)
            {
                AttributeCollection attributes 
    = TypeDescriptor.GetProperties(new MyClass())["MyProperty"].Attributes;

                
    /* Prints the default value by retrieving the DefaultValueAttribute 
                 * from the AttributeCollection. 
    */
                DefaultValueAttribute myAttribute 
    =
                   (DefaultValueAttribute)attributes[
    typeof(DefaultValueAttribute)];
                Console.WriteLine(
    "The default value is: " + myAttribute.Value.ToString());

                Console.Read();
            }

            
    class MyClass
            {
                
    private bool myVal = false;

                [DefaultValue(
    false)]
                
    public bool MyProperty
                {
                    
    get
                    {
                        
    return myVal;
                    }
                    
    set
                    {
                        myVal 
    = value;
                    }
                }

            }

              这是MSDN例子,注意看两点:

                   1.     DefaultValue传入参数为false,而myVal设置的也是false

                   2.     再看调用的地方,是使用的DefaultValueAttribute,而不是直接访问的属性MyProperty

              狐疑了吧,他设置的默认值不是属性MyProperty的默认值!!不信的话你可以把[DefaultValue(false)]改成[DefaultValue(true)],然后直接Console.WriteLine(new MyClass().MyProperty);!!

              从上面参考的几篇文章中可以发现,Attribute可以做方法参数校验、也可以为过时的方法做标记 ObsoleteAttribute为方法传参,虽然如此,但是仍然觉的使用起来并不方便,只是看起来比较酷罢了,一个类居然可以这样使用!!可以把这个类仅仅使用在类、方法、字段上,也可以使用所以地方,控制使用次数,显然仍然是有他的用处的,没法把你的代码和他耦合起来,也是用反射来实现相关功能的!!

              说到这里实在还是不太明白,但是起码知道DefaultValueAttribute不是为当前类设置默认值的,所以使用的时候注意Attribute概念问题!欢迎大家提意见: )

  • 相关阅读:
    LeetCode(274)H-Index
    LeetCode(279)Perfect Squares
    LeetCode(278)First Bad Version
    LeetCode(289)Game of Life
    LeetCode(292) Nim Game
    LeetCode(282) Peeking Iterator
    解决Secure Shell Client(SSH)客户端中文乱码的方法
    Mysql 或者当前时间戳
    java http请求url报错505,但是浏览器可以正常返回json数据
    查询直播流实时帧率和码率 签名
  • 原文地址:https://www.cnblogs.com/over140/p/1334584.html
Copyright © 2011-2022 走看看