REFLECTION

zhaozj2021-02-16  32

Access custom properties

When the property is associated with the program element, reflection can be used to query whether they exist and their values. The main reflection method for query properties is included in System.Reflection.memberInfo.getCustomAttributes and System.Reflection.Assembly.getCustomattributes.

Accessibility of custom properties is checked in accordance with the assembly of this property. This is equivalent to checking whether the method on the type in the program set in addition to custom attributes can call the constructor of the custom attribute.

The visibility and accessibility of type parameters such as System.Reflection.Assembly.getCustomattributes (Type, Boolean) are checked. Only code in the program set containing the user-defined type can be retrieved using getCustomAttributes to retrieve this type of custom properties.

The following code example is a typical custom property design pattern. It describes the runtime custom property reflection model.

[C #]

SYSTEM.DLL

Public Class DescriptionAttribute: Attribute

{

}

System.Web.dll

Internal Class MyDescriptionAttribute: DescriptionAttribute

{

}

Public Class LocalizationExtenderProvider

{

[MyDescriptionAttribute (...)]

Public CultureInfo getLanguage (...)

{

}

}

If you try to retrieve custom properties for public custom property types that are attached to the getLanguage method, the run library will do the following:

The run library checks for type.getCustomAttributes (Type Type) is a common and checks if it is visible or can be accessed.

Running checks from DescriptionAttribute, user-defined type MyDescriptionAttribute is visible and accessible within the System.Web.dll assembly (which is attached to the GetLanguage () method).

The run library Check if the constructor of MyDescriptionAttribute is visible and accessible in the System.Web.dll program.

Run library calls the constructor of MyDescriptionAttribute with custom property parameters, and then returns the new object to the call.

Custom Properties Reflective Models may have an instance of the user-defined type of programs in the defined type. This is the same as a member of the instance of the user-defined type (for example, returning the type.getMethods () of the RuntimeMethodInfo object array. To prevent clients from discovering information about user-defined custom attribute types, the members of this type are defined as non-public members.

The following code example illustrates the basic method of using reflection access to custom properties.

[C #]

Class mainclass

{

Public static void main ()

{

System.Reflection.memberInfo info = typeof (myclass);

Object [] attributes = info.getcustomattributes ();

For (int i = 0; i

{

System.Console.writeline (Attributes [i]);

}

}

}

Specify a fully qualified type name

To provide a valid input for a variety of reflection operations, you must specify the type name. A fully qualified type name contains the program set name specified, named space specified, and type name. Type Name Specify will be used by type.gettype, module.gettype, modulebuilder.gettype, and askEMBLY.GETTYPE. BACKUS-NAUR Form Syntax of Type Name

Backus-Naur Form (BNF) Defines the syntax of the formal language. The BNF in the table below will explain how to identify valid inputs. The final element (the elements that cannot be reduced will be displayed all in uppercase letters. Non-final elements (which can be reduced) are displayed as uppercase mixing or single quotation strings, but single quotes (') are not part of the grammar itself. The pipe character (|) indicates a rule with a subregion.

BNF syntax for fully qualified type name

TypeSpec: = ReferencetypeSpec

| SimpleTypeSpec

Referencetypespec: = SimpleTypeSpec '&'

SimpleTypeSpec: = POINTERTYPESPEC

| ArrayTypeSpec

| TypeName

PointertypeSpec: = SimpleTypeSpec '*'

ArrayTypeSpec: = SimpleTypeSpec '[REFLECTIONDIMENSION]'

| SimpleTypeSpec '[ReflectionEmitDimension]'

ReflectionDimension: = '*'

| ReflectionDimension ',' ReflectionDimension

| NOTOKEN

ReflectionEmitDimension: = '*'

| Number '..' Number

| Number '...'

| ReflectionDimension ',' ReflectionDimension

| NOTOKEN

Number: = [0-9]

TypeName: = namespacetypename

| Namespacetypename ',' assemblynamespec

Namespacetypename: = NESTEDTYPENAME

| Namespacespec '.'nestedTypename

NESTEDTYPENAME: = Identifier

| NESTEDTYPENAME ' ' IDENTIFIER

Namespacespec: = Identifier

| Namespacespec '.'Identifier

AskMBLYNAMESPEC: = Identifier

| Identifier ',' askEMBLYPROPERTIES

AskEMBLYPROPERTIES: = askPLYPROPERTY

| AssemblyProperties ',' AssemblyProperty

AskEMBLYPROPERTY: = askPROPERTYNAME '=' askPLYPROPERTYVALUE

Special character

In Typename, Identifier is any valid name determined by the language rules.

The backslash (/) can be used as an escaping to separate the marks used as part of the Identifier.

mark

meaning

/,

Program set separator.

/

Nested type separator.

/ &

Quote type.

/ *

Pointer type.

/ [

Array dimensional separator.

/]

Array dimensional separator.

/.

A backslash should only be used before the group is used in the array specified. The period in NameSpacesPec does not use a backslash.

//

Used as a backslash of the string.

Note that in all TypeSpec components other than AssemblyNameSpec, spaces are related. In AssemblyNameSpec, "," the space before the separator is related, but "," the space after the separator will be ignored.

The reflective class (such as type.fullname) will return the processed name so that the returned name can be used in the call to getType (eg in mytype.gettype (myType.FullName).

For example, a fully qualified name of a type may be ozzy.outback.kangaroo wallaby, myassembly.

If the namespace is Ozzy.out Back, you must add a slope before the plus sign. Otherwise, the analyzer will explain it as a nested separator. Reflection will use the string as ozzy.out / back.kangaroo wallaby, myassembly.

Specify an assembly name

The minimum information required for the assembly name specified is the text name (Identifier) ​​of the assembly. You can add the comma-separated property / value pair list described in the table below after Identifier. Identifier naming should follow the rules naming file name. Identifier is not case sensitive.

Effective assembly properties

Attribute name

Description

allowance

Version

Context version number

Major.minor.build.Revision, where Major, Minor, Build and Revision are integers between 0 and 65535 (including 0 and 65535).

Publickey

Full public key

The hexadecimal string value of the full public key. Specifies NULL (Noth in Microsoft Visual Basic .NET) Explicitly indicates private assemblies.

PublicKeyToken

Public key tag (8-byte hash of full public key)

The hexadecimal string value of the public key tag. Specifies NULL (Nothing in Visual Basic .NET) Explicitly indicates private assemblies.

Culture

Assembly area

The RFC-1766 format area of ​​the assembly, or "non-specific" sets for independent of language (non-attached).

Custom

Custom binary large object (blob). It is currently only used by the assembly generated by the native image generator (NGEN.exe).

Custom string, by native image generator tool to notify the program cache to notify the installed assembly to the native image, so it will be installed in the native image cache. Also known as a ZAP string.

The following code example shows AssemblyName with the default area and a simple name simple assembly. [C #]

com.microsoft.crypto, culture = ""

The following code example displays a fully qualified reference to the assembly of regionality "en" and with a strong name.

[C #]

Com.microsoft.crypto, Culture = En, PublickeyToken = A5D015C7D5A0B012,

Version = 1.0.0.0

The following code sample displays the section specified asMblyName, which can be satisfied by a set with a strong name or simple name.

[C #]

com.microsoft.crypto

com.microsoft.crypto, culture = ""

com.microsoft.crypto, culture = en

Each code example shows a part of the specified AssemblyName, which must be satisfied by an assembly with a simple name.

[C #]

COM.MICROSoft.crypto, Culture = "", publickeytoken = NULL

com.microsoft.crypto, culture = en, publickeytoke = null

Each code example shows a partially specified AssemblyName, which must be satisfied by a set of strong names.

[C #]

com.microsoft.crypto, culture = "", publickeyToken = A5D015C7D5A0B012

Com.microsoft.crypto, Culture = En, PublickeyToken = A5D015C7D5A0B012,

Version = 1.0.0.0

Specified pointer

SIMPLETYPESPEC * indicates a non-hosting pointer. For example, to get a pointer to the MyType type, use type.gettype ("mytype *"). To get a pointer to the MyType type pointer, use Type.gettype ("mytype **").

Specified reference

SIMPLETYPESPEC & indicates a managed pointer or reference. For example, to get a reference to the MyType type, use Type.gettype ("MyType &"). Note that different points are limited to one level.

Specified array

In the BNF syntax, ReflectionEmitDimension applies only to incomplete type definitions that use modulebuilder.gettype. Incomplete type definitions are the TypeBuilder objects constructed using the Reflection.emit (but do not call TypeBuilder.createType for these objects). ReflectionDimension can be used to retrieve any completed type definition, that is, the type of load has been loaded.

By specifying the rank of the array, the arrays in the reflection can be accessed.

TYPE.GETTYPE ("MyArray []") obtains a single-dimensional array of lower limit of 0.

Type.getType ("MyArray [*]") obtains a single-dimensional array of unknown unknown.

TYPE.GETTYPE ("MyArray []") obtains an array of two-dimensional arrays.

TYPE.GETTYPE ("MyArray [*, *]") and Type.getType ("MyArray [,]") get the lower limit unknown rectangular two-dimensional array.

Please note that from the point of view of the running library, MyArray []! = MyArray [*], but for multi-dimensional arrameters, both are equivalent. That is to say, Type.gettype ("MyArray [,]") == Type.gettype ("MyArray [*, *]" calculation results are TRUE. For modulebuilder.gettype, myArray [0..5] indicates a single-dimensional array of size 6 lower than 0. MyArray [4 ...] indicates a single-dimensional array that is unknown with a lower limit of 4.

to sum up

The above is some methods and code examples of the .NET runtime to understand the type information, for your reference only. If you have any suggestions, please Mil, Billcheng@citiz.net

转载请注明原文地址:https://www.9cbs.com/read-24469.html

New Post(0)