Home Random Page


CATEGORIES:

BiologyChemistryConstructionCultureEcologyEconomyElectronicsFinanceGeographyHistoryInformaticsLawMathematicsMechanicsMedicineOtherPedagogyPhilosophyPhysicsPolicyPsychologySociologySportTourism






Nbsp;   Attribute Constructor and Field/Property Data Types

When defining your own custom attribute class, you can define its constructor to take parameters that must be specified by developers when they apply an instance of your attribute type. In addition, you can define nonstatic public fields and properties in your type that identify settings that a devel- oper can optionally choose for an instance of your attribute class.

When defining an attribute class’s instance constructor, fields, and properties, you must restrict yourself to a small subset of data types. Specifically, the legal set of data types is limited to the fol- lowing: Boolean, Char, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Single, Dou­ ble, String, Type, Object, or an enumerated type. In addition, you can use a single-dimensional,


zero-based array of any of these types. However, you should avoid using arrays because a custom attribute class whose constructor takes an array is not CLS-compliant.

When applying an attribute, you must pass a compile-time constant expression that matches the type defined by the attribute class. Wherever the attribute class defines a Type parameter, Type field, or Type property, you must use C#’s typeof operator, as shown in the following code. Wherever

the attribute class defines an Object parameter, Object field, or Object property, you can pass an Int32, String, or any other constant expression (including null). If the constant expression rep- resents a value type, the value type will be boxed at run time when an instance of the attribute is constructed.

Here’s an example of an attribute and its usage.

 

using System;

internal enum Color { Red } [AttributeUsage(AttributeTargets.All)]

internal sealed class SomeAttribute : Attribute {

public SomeAttribute(String name, Object o, Type[] types) {

// 'name' refers to a String

// 'o' refers to one of the legal types (boxing if necessary)

// 'types' refers to a 1­dimension, 0­based array of Types

}

}

 

[Some("Jeff", Color.Red, new Type[] { typeof(Math), typeof(Console) })] internal sealed class SomeType {

}

 

Logically, when a compiler detects a custom attribute applied to a target, the compiler constructs an instance of the attribute class by calling its constructor, passing it any specified parameters. Then the compiler initializes any public fields and properties by using the values specified via the enhanced constructor syntax. Now that the custom attribute object is initialized, the compiler serializes the at- tribute object’s state out to the target’s metadata table entry.

       
   
 
 



Date: 2016-03-03; view: 749


<== previous page | next page ==>
Nbsp;   Defining Your Own Attribute Class | Nbsp;   Detecting the Use of a Custom Attribute
doclecture.net - lectures - 2014-2024 year. Copyright infringement or personal data (0.006 sec.)