Chapter 2 reflection
1. Custom Attributes)
The processing process of the compiler when a custom feature element is applied in the Code:
eg:[StringLength(50, ErrorMessageResourceType = typeof(res), ErrorMessageResourceName = "MAX_USER_NAME_LENGTH")]public String UName { get; set; }
// When the C # compiler finds that this property applies a StringLength feature, it first adds the string Attribute to the end of the name to form a combination name StringLengthAttribute, then, search for the class with the specified name in all the namespaces in the search path (that is, the namespace mentioned in the using Statement. Note that if a feature is used to mark a data item and the feature name ends with a string Attribute, the compiler does not add the string to the combination name, but does not modify the feature name.
// The compiler will find the class containing this name, and this class is directly or indirectly derived from System. Attribute. The compiler also believes that this class contains information about the usage of control features, especially:
A. What types of program elements can a feature be applied to (class, structure, attributes, and methods)
B. Can it be applied to the same program element multiple times?
C. Whether the derived class and interface inherit the feature when it is applied to a class or interface
D. required and optional parameters for this feature
---- If the compiler cannot find the corresponding feature class or finds such a feature class, but the method of using the feature does not match the information in the feature class, the compiler will generate a compilation error.
2. AttributeUsage feature class
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = false)] public class FieldNameAttribute : Attribute { public FieldNameAttribute() { } }
// A custom feature class FieldNameAttribute is marked with a System. AttributeUsage feature class. The AttributeUsage feature class is used to identify the types of program elements that a custom feature can apply. This information is provided by its first parameter. this parameter is required and belongs to the enumeration type AttributeTargets.
// The AttributeTargets enumeration member has two values that are inconsistent with any program element: Assembly and Module, indicating that this feature can be applied to the entire Assembly or Module, rather than to an element in the code, in this case, this feature can be stored anywhere in the source code, but you need to use the keyword assembly or module as the prefix.
[assembly: SomeAssemblyAttribute(Parameters)] [module: SomeAssemblyAttribute(Parameters)]
3. System. Type class
A Type class is an abstract base class. As long as a Type object is instantiated, a Type derived class is actually instantiated. A Type has a derived class corresponding to each data Type.
Generally, there are three common methods to obtain Type references pointing to any given Type:
A. Use the typeof operator of C #
// Obtain a certain Type of System. Type object in the compilation phase
// The typeof () parameter can only be a defined type and custom type in the framework, not an instance
Type t = typeof (double); B. Using the GetType () method, all classes will inherit this method from System. Object // obtain the Type of the current instance in the runtime stage
// GetType () is the instance method of double d = 10.0; Type t = d. getType (); c. call the static method GetType () Type t = Type. getType ("System. double ");
// Type is the entry of many reflection functions. It implements many methods and attributes. You can use Type to determine the data Type, but cannot use it to modify the Type.
Type can also be used to obtain references to the Assembly that defines this Type:
Type t = typeof(Vector);Assembly ass = new Assembly(t);
4. Assembly class
The Assembly class allows access to the metadata of a given Assembly. It also contains methods for loading and executing the Assembly [assuming that the Assembly is executable ]. Like the Type class, the Assembly class contains many methods and attributes.
Before using the Assembly instance to do some work, you need to load the corresponding Assembly into the running process. To do this, you can use static members Assembly. Load () or Assembly. LoadFrom ().
A. Assembly. Load () ---- the parameter of this method is the Assembly name. The runtime will search for the Assembly in various locations, including the local directory and Global Assembly cache.
B. Assembly. LoadFrom () ---- the parameter of this method is the complete path name of the Assembly. It does not search for the Assembly elsewhere.
Assembly ass1 = Assembly.Load("SomeAssembly");Assembly ass2 = Assembly.LoadFrom(@"C:\My Projects\software\SomeotherAssembly");
After an assembly is loaded, you can use its various attributes for query, such:
A. type [] types = theAssembly. getTypes (); foreach (Type t in types ){...} b. search for custom features // obtain all custom feature attributes in the specified dataset [] attr = Attribute. getCustomAttributes (theAssembly );