Directory: 1 What is characteristic? What is the use of the why feature (why usage features) 3 How to use Features 4 Retrieve information stored in properties
1 What is characteristic:
Characteristic overview
Features are descriptive tags that provide additional information about programming elements (such as types, fields, methods, and attributes). Other applications (such as Visual Basic Compiler) can lead
Use other information in characteristics to determine how to use these items.
Features and metadata
Features Save together with the "metadata" of the Visual Basic .NET assembly. Metadata is information describing each element of the running library host. This can include debugging and garbage
Receive the information you need, including security features, blocking processing data, extended classes, and member definitions, version binding, and any other information needed at runtime. The method of using characteristic specified metadata is similar to keywords using public and private to provide information about access level. However, different from keywords
Most of the characteristics are not specific to specific languages. Usage features can extend the functionality of the Visual Basic language without changing the compiler.
Features and performance
Some of the points of the feature include: one or more features can be applied to the entire assembly, module, or smaller program elements (such as classes and attributes). Features can accept parameters in the same way as methods and properties. Characteristics are divided into two groups: .NET Framework and Visual Basic .NET use features and only customizen for specific applications. Although .NET Framework contains many useful features, but if necessary, you can define your own custom features. Custom features are defined in the characteristic class based on the System.attribute class. Feature class itself uses AttributeUSAGE to provide additional information about how to use features
. The process of retrieving metadata from characteristics is called "reflection". Reflection involves using tools to allow objects to retrieve and check metadata about their own members.
Characteristic parameters: optional parameters, required parameters, positioning parameters, named parameters
Positioning parameters: Parameters that are specified by the parameter
Named Parameters: Set the value of the properties and fields directly, add ": =" by the ribbon parameter name to specify the named parameter
2. What is the use of characteristics?
Common use of characteristics
Sometimes you can use when running libraries and class libraries using a public language. Several common uses of the following list:
In XML Web Services, use the WebMethod feature tag method to indicate that the method should be called via the SOAP protocol. For more information, see
WebMethodAttribute class. Description How to block the method parameters when interacting with the native code. For more information, see the Marshalasattribute class. Describe the COM properties of classes, methods, and interfaces. Tag components as COM so that the Visual Basic compiler generates additional code required to create COM components. See the COMCLASSATTRIBUTE class for more information. Use the DLLIMPORTATTRIBUTE class to call unmanaged code. Describe your assembly in terms of headlines, versions, descriptions, or trademarks. Describe which members of the sustainable serialization class. Describe how to map classes and XML nodes for XML serialization. Describe the safety requirements of the method. Specifies the characteristics used to force the security. Optimized by real-time (JIT) compiler to easily debug code. You can use a variety of other ways to use features, you can also create custom features.
3 How to use features to add feature blocks to program elements (attributes, methods, events, classes, and assemblies). The feature block consists of angle brackets, including comma-separated list of characteristic declarations. Features Declare consisting of an optional feature modifier (such as Module, Assembly), a list, a list of positioning parameters, and optional named parameters. A modifier characteristic must be placed in the characteristic portion of the top of the source file. The following code is set to the assembly characteristic of the assembly and the indication module complies with the "Public Language Specification" module feature: option Explicit on Imports System.Reflection
You can also apply assembly feature through AssemblyInfo.vb file.
When applied to program elements such as attributes, the characteristics take precedence over the element
According to the convention, all characteristic names are ended in words "Attribute" to distinguish them with other items in ".NET Framework", but do not need to specify when using feature
Sexual suffix.
EXP
The following example definition can only be applied to class custom features:
'********************************************************** *********************************************************** *
'Declare Two Private Fields to Store The Property Values. Private M_Label AS String Private M_Value AS INTEGER
'The Sub New Constructor Is The Only Way To Set The Properties. Public Sub New (Byval_Label As String, Byval _Value As Integer) M_Label = _Label M_Value = _VALUE End Sub
Public Overridable Readonly Property Label () AS STRING GET RETURN M_LABEL END GET End Property
Public Overridable Readonly Property Value () AS Integer Get ProPETURN M_VALUE END GET End Property End Class Only the constructor of the feature class can set the properties defined in this feature. The following code shows how to use this feature:
'Apply The Custom Attribute To this class.
'********************************************************** *********************************************************** * 4 Retrieving information stored in the properties First declares the retrieved attribute instance and use the Attribute.getCustomattribute method to initialize the new attribute to the attribute value to retrieve. After initialization, you only need to use its property value to get the value.
Retrieving a custom property applied to a specified type of assembly or a specified type inherited from the base class.
Overloads Public Shared Function GetCustomAttribute (Assembly, Type) AS Attribute Retrieves a custom property of a specified member applied to class or a specified type of the specified type inherited from the base class.
Overloads Public Shared Function GetCustomAttribute (MemberInfo, Type) AS Attribute Retrieves custom properties for specified types that are applied to specify modules or from base classes.
Overloads Public Shared Function GetCustomAttribute (Module, Type) AS Attribute Retrieves the specified parameter applied to class members or custom properties of the specified type inherited from the base class.
Overloads Public Shared Function GetCustomAttribute (ParameterInfo, Type) AS Attribute Retrieves custom properties of specified types that apply to specify the assembly or inheritance (optional) from the base class.
Overloads Public Shared Function GetCustomAttribute (Assembly, Type, Boolean) AS Attribute Retrieves the specified member of the class or the specified type of the specified type of the base class (optional).
Overloads Public Shared Function GetCustomAttribute (MemberInfo, Type, Boolean) AS Attribute Retrieves the specified type of custom property applied to the specified module or from the base class inheritance (optional).
OverLoads Public Shared Function GetCustomAttribute (Module, Type, Boolean) AS Attribute Retrieves the specified parameter applied to class members or the specified type of the specified type of the specified type of the base class (optional).
Overloads Public Shared Function GetCustomattribute (ParameterInfo, Type, Boolean) AS Attribute
EXP '*********************************************************** *********************************************************** ** [Visual Basic]
Imports System
( ". The attribute was not found") If MyAttribute Is Nothing Then Console.WriteLine Else Console.WriteLine ( "The Name Attribute is:. {0}", MyAttribute.Name) Console.WriteLine ( "The Level Attribute is: { 0}. ", Myattribute.level) Console.writeline (" The Reviewed Attribute IS: {0}. ", Myattribute.reviewed) end if End suend class' *************** *********************************************************** ********************************** Note 1: GetType used in the previous example
The GetType operator returns the TYPE object of the specified type, and the Type object can be used to retrieve information about type, such as type properties, methods, and events. Gettype (Typename)
Note 2: The result of the query for the Type class is:
Type is the root of the System.Reflection function, but also the main way of accessing metadata. Using Type's membership information about type declarations, such as constructor, methods, fields, properties, and classes of events, and modules and assemblies deploying this class.
If there is no ReflectionPermission, the code can only access public members of the loaded assembly. This includes, but is not limited to, an unrestricted access to Object.gettype, access to public export types through Type.gettype, and GetTypeFromHandle
Access. Some attributes of Type (such as fullname and attributes) can be accessed without ReflectionPerMission. TYPE is an abstract base class that allows multiple implementations. The system will always be available to the party RuntimeType. In the reflection, all the classes at the beginning of the runtime can only create each object in the system, and these classes support comparison operations. ********************************** The query result for the ReflectionPermission class is: System.Object System.security. CodeAccessPermission System.Security.Permissions.ReflectionPermission
The System.Type class is a core role in reflecting. When the type of reflection request is loaded, the public language runtime will create a Type for it. You can use the Type object method, field, attribute, and nested to find this type. All information
When using Assembly.getType or Assembly.gettype, you can get the name of the required type, you can get the Type object from the unloaded program to get the Type.gettype to get the Type object from the loaded program to use Module.gettype and Module. gettypes available signature module type object type class as follows: System.Object System.Reflection.MemberInfo System.Type System.Reflection.Emit.EnumBuilder System.Reflection.Emit.TypeBuilder System.Reflection.TypeDelegator