Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

CSHARP ATTRIBUTES ~ mkniit

C# ATTRIBUTES

Attributes are declarative tags class, methods, structures, enumerators, the Assembly dll program on a wide variety of information about the behavior of the element is used to pass at run time. Using attributes, you can add a declarative program information. Described as square brackets () in the declarative markup to be used will be placed on top.

The compiler instructions such as metadata and comments, descriptions, methods, and other information, such as a class for the program is used to add the attribute. .NET framework, there are two types of characters: predefined attributes and Custom attributes.

Specify the Attributes

The syntax for specifying attributes are as follows:


Specify the Attributes in c#

The attribute names and values noted in parentheses before the element that the attribute is applied. Positional parameter specifies important information and specify the name of an optional parameter information.

Predefined Attributes

.NET Framework provides three attributes set: 
  • The use of attribute
  • Conditional
  • Obsolete 


AttributeUsage

The pre-defined attribute AttributeUsage describes how a Custom Attribute class can be used. It specifies the types of items to which the attribute can be applied.

Attribute Usage in c#

Syntax for specifying this attribute is as follows:
 Where,
  • Determine the language of the element attribute of the Validon parameter. Is a combination of AttributeTargets counter value. The default is the AttributeTargets. All.
  • Allowmultiple parameter (optional) Boolean AllowMultiple property value provides values for this attribute. This is true, the attribute is multiuse. The default value is false (disposable).
  • Parameter inheritance (optional) this attribute is a Boolean value for an inherited property value. If it is true, the attribute is inherited by derived classes. The default value is false (not inherited).
For example, 

Example predefined attributes in c#

 Conditional

This attribute depends on the specific preprocessing identifier standard conditional execution method.

Then, depending on the value of the debug or trace method calls a specific conditional compilation. For example, this code displays the value of a variable while debugging.

The syntax for specifying these attributes are as follows:


Conditional attribute c#

For example,

example conditional attributes c#

 The following example demonstrates the attribute:

example syntax conditional attributes c#

When the above code is compiled and executed, it produces the following result:

result of example conditional attributes c#

Obsolete
 
You cannot use this attribute displays the program entities, standards. This allows the compiler to ignore the elements of a specific target. For example, the new method is being used in a class, and still want to keep the existing method of a class, you can display it as an old message is displayed instead of the old method, you must use the new method.

The syntax for specifying these attributes are as follows:

Obselete attributes c#

 Where,

The message parameter is not used, what is a string describing the reason instead.

A Boolean value that specifies the Iserror parameter. A value of true indicates that the compiler should treat the use of the item as an error. The default value is false (compiler generates a warning).

The following program demonstrates this:

example obselete attribute c#

 When you try to compile the program, the compiler gives an error message stating:

result of example obselete attributes c#

 Creating Custom Attributes

.NET framework has a custom attribute to the declarative information can be used to store and can be retrieved at run time, you can generate. This information design standards, and for each target element, depending on the application needs can be associated with.

Create and use custom attributes to four steps:
  • A custom attribute declaration
  • Write a custom attribute
  • Applies a custom attribute to the element in the target program
  • Accessing attributes through reflection
The final step is to read the metadata of a variety of simple notation program created includes. Metadata is data or other data used to describe the data. The program at run time, you must use reflection to access the attribute. We are in the next chapter.

Declaring a Custom Attribute
 
A new custom attribute should is derived from the System.Attribute class. For example,

Declaring a custom attribute

 In the code we previously declared a custom attribute named DeBugInfo.

Constructing the Custom Attribute

We debug a program called DeBugInfo, the information obtained through custom attributes. Let's take a look at the following information store:
  • The code for the number of bugs
  • The name of the developers identify bugs
  • Code of the last review date
  • To store the message string developer statement
Class DeBugInfo property information when receiving or peeking a message the first three stores and public properties store. The following bug number, date, and location is the name of the developer of the parameter class with an optional parameter of the message or the review DeBugInfo named.

Each attribute must have at least one constructor. Positional parameters must be passed through a constructor. The following code shows DeBugInfo class.

custom attributes c#

Applying the Custom Attribute
 
The attribute is applied by placing it immediately before its target:

applying custom attributes c#



This post first appeared on GNIITSolution, please read the originial post: here

Share the post

CSHARP ATTRIBUTES ~ mkniit

×

Subscribe to Gniitsolution

Get updates delivered right to your inbox!

Thank you for your subscription

×