浅谈Attribute [C# | Attribute | DefaultValueAttribute]

简介:

正文:

          先看我的错误代码

        [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概念问题!欢迎大家提意见: )


本文转自over140 51CTO博客,原文链接:http://blog.51cto.com/over140/586471,如需转载请自行联系原作者

相关文章
|
10月前
Warning: [antd: Form.Item] `defaultValue` will not work on controlled Field. You should use `initialValues` of Form instead.
Warning: [antd: Form.Item] `defaultValue` will not work on controlled Field. You should use `initialValues` of Form instead.
294 0
|
10月前
|
数据库
Field ‘id‘ doesn‘t have a default value
Field ‘id‘ doesn‘t have a default value
117 0
AttributeError: 'module' object has no attribute 'X509_up_ref'
AttributeError: 'module' object has no attribute 'X509_up_ref'
134 0
|
TensorFlow 算法框架/工具
has no attribute ‘swish‘
has no attribute ‘swish‘
135 0
AttributeError: EfficientDet object has no attribute module
AttributeError: EfficientDet object has no attribute module
225 0
成功解决AttributeError: module 'enum' has no attribute 'IntFlag'?
成功解决AttributeError: module 'enum' has no attribute 'IntFlag'?
成功解决AttributeError: module 'enum' has no attribute 'IntFlag'?