Introduction to Attribute [C # | Attribute | DefaultValueAttribute]

Source: Internet
Author: User

 

Preface

Recently, permission control was implemented to use IHttpModule for page permission control. To refine the control granularity to the permission judgment of the control on the page, the intention is to pass in the control number, based on the control number and permissions of the current user, if you do not have the permission, you can hide or display the control as unavailable and intend to use Attribute. The following are some problems and opinions encountered, although it failed, it still seems to be of reference value.

 

Several articles recommended:

1. Application of Attribute in. net Programming (1)

2. Application of Attribute in. net Programming (2)

3. Application of Attribute in. NET Programming (3)

4. "href =" http://dudu.cnblogs.com/articles/4452.html "> application of Attribute in. NET Programming (4)

5. CLR notes: 17. Custom Attributes

 

Body:

View me firstError Code:

[AttributeUsage (AttributeTargets. Class | AttributeTargets. Field | AttributeTargets. Method)]
Public class PowerAttribute: Attribute
{
/// <Summary>
/// Determine the permissions of the control on the page. The string of the permission is URL + "? Id "+ Control. ID
/// For example:/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; -- I> = 0 ;)
{
WebControl Control = control. GetValue (I) as WebControl;
If (Control! = Null)
If (HasPower (string. Concat (WebHelper. WebRequest. Path ,"? Id = ", Control. ID) =-1) // determine the permission
If (pType = PowerType. Visible)
Control. Visible = false;
Else
Control. Enabled = false;
}
}

Public int HasPower (string path)
{
Return-1;

}

/// <Summary>
/// Obtain control permissions. 1 has permissions, 0 users time out, and-1 has no permissions.
/// </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>
/// Obtain the page permission. 1 has the permission, 0 has timed out, and-1 has no permission.
/// </Summary>
/// <Param name = "control"> </param>
/// <Param name = "Field"> </param>
Public PowerAttribute (ref int Field)
{
Field = HasPower (WebHelper. WebRequest. Path );
}
}

The intent is obvious. If you want to add an Attribute in the page to control the control permission, you can automatically set it to the specified display status, but it failed! The following error message is displayed:

Attribute parameters must be constant expressions, typeof expressions, or array creation expressions ConsoleTest

That is to say, it cannot be passed, nor can it use ref to pass references! That is to say, there is no wayDirectChanging the values of controls or fields on the page reminds me of DefaultValueAttribute. The MSDN explanation is:

Specifies the default value of a Property. I found that many of my friends have misunderstood this. first look at the Code:

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 =
(Defavaluvalueattribute) 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;
}
}

}

This is an MSDN example. Pay attention to the following two points:

1. The DefaultValue parameter is set to false, while myVal is set to false.

2. Check that the DefaultValueAttribute is used instead of the directly accessed property MyProperty.

Suspect that the default value he set is not the default value of the property MyProperty !! If you don't believe it, you can change [DefaultValue (false)] to [DefaultValue (true)], and then directly Console. WriteLine (new MyClass (). MyProperty );!!

From the articles referenced above, we can find that Attribute can be used for method parameter verification or marking outdated methods.ObsoleteAttribute,Although this is the case for passing parameters for methods, it is not convenient to use them, but it looks cool. A class can be used in this way !! This class can be used only for classes, methods, and fields. It can also be used to control the number of times of use, which is obviously still useful, you can't couple your code with him. Reflection is also used to implement related functions !!

I still don't quite understand it here, but at least I know that DefaultValueAttribute does not set the default value for the current class, so pay attention to the Attribute concept when using it! Comments are welcome :)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.