"C #". NET what types are available to implement reflection

Source: Internet
Author: User

Most of the types that implement reflection are defined under the system.reflection namespace.

Assembly defines a Assembly, which is a reusable, version-free, and self-describing common language runtime application building block.

AssemblyName Unique identification of the complete description assembly

EventInfo discovers the properties of an event (Attribute) and provides access to event metadata

FieldInfo Discovery Field Properties (Attribute) and provides access to field metadata

Localvariableinfo discovers the properties of local variables and provides access to local variable metadata

Manifestresourceinfo contains manifest resource topology information

MemberInfo get information about member properties and provide access to member metadata

MethodBase provides information about methods and constructors

MethodBody provides access to metadata and MSIL for the method body

Module performs reflection on modules

ParameterInfo discovers parameter properties (Attribute) and provides access to parameter metadata

PropertyInfo attribute (property) properties (Attribute) and provides access to property metadata

Example:

usingSystem;usingSystem.Reflection;namespaceusereflection{Partial classusereflection {/// <summary>        ///Analyzing Assemblies/// </summary>        /// <param name= "assembly" ></param>        Private Static voidanalyzeassembly (Assembly Assembly) {//Print the name of the AssemblyConsole.WriteLine ("assembly Name:"+Assembly.            FullName); //Print the location of the AssemblyConsole.WriteLine ("Assembly Location:"+Assembly.            Location); //whether the print assembly exists in the GACConsole.WriteLine ("whether the assembly exists in the GAC:"+Assembly.            Globalassemblycache.tostring ()); //to print a list of modules that contain an assembly manifestConsole.WriteLine ("the module that contains the assembly manifest:", assembly.            Manifestmodule.name); //print the CLR version of an assemblyConsole.WriteLine ("to run the CLR version that the assembly requires:", assembly.            Imageruntimeversion); Console.WriteLine ("now start parsing the referenced assembly"); Module[] Modules=Assembly.            GetModules (); foreach(Module moduleinchmodules)            {analyzemodule (module); }        }        /// <summary>        ///Analysis Module/// </summary>        /// <param name= "module" ></param>        Private Static voidanalyzemodule (module module) {Console.WriteLine ("Module Name:"+module.            Name); Console.WriteLine ("the UUID of the module:"+module.            Moduleversionid); Console.WriteLine ("start the type under Analysis module"); Type[] Types=module.            GetTypes (); foreach(Type typeinchtypes)            {Analyzetype (type); }        }        /// <summary>        ///Type of Analysis/// </summary>        /// <param name= "type" ></param>        Private Static voidanalyzetype (Type type) {Console.WriteLine ("type name:"+type.            Name); Console.WriteLine ("type of Category:"+type.            Attributes); if(Type. BaseType! =NULL) {Console.WriteLine ("the base class for the type is:"+type.            Basetype.name); } Console.WriteLine ("GUID of type:"+type.            GUID); //set type members of interestBindingFlags flags = (BindingFlags.NonPublic | BindingFlags.Public | bindingflags.static |bindingflags.instance); //Analyze Membersfieldinfo[] Fields =type.            GetFields (flags); if(Fields. Length >0) {Console.WriteLine ("start parsing members of type"); foreach(FieldInfo FieldinchFields )                {Analyzefield (field); }            }            //methods included in the analysisMethodinfo[] Methods =type.            GetMethods (flags); if(Methods. Length >0) {Console.WriteLine ("methods to start analyzing types"); foreach(MethodInfo methodinchmethods)                {Analyzemethod (method); }            }            //Analysis PropertiesPropertyinfo[] Properties =type.            GetProperties (flags); if(Properties. Length >0) {Console.WriteLine ("start parsing the properties of a type"); foreach(PropertyInfo Propertyinchproperties)                {Analyzeproperty (property); }            }        }        /// <summary>        ///Analyze Members/// </summary>        /// <param name= "field" ></param>        Private Static voidAnalyzefield (FieldInfo field) {Console.WriteLine ("member Name:"+field.            Name); Console.WriteLine ("categories of Members:"+field.            Attributes); Console.WriteLine ("type name of the member:"+field.        Fieldtype.name); }        /// <summary>        ///Analysis Method/// </summary>        /// <param name= "method" ></param>        Private Static voidAnalyzemethod (MethodInfo method) {Console.WriteLine ("Method Name:"+method.            Name); Console.WriteLine ("categories of methods:"+method.            Attributes); Console.WriteLine ("parameters for starting analysis methods"); parameterinfo[] Parameters=method.            GetParameters (); if(Parameters. Length <=0) {Console.WriteLine ("method has no parameters"); }            foreach(ParameterInfo parameterinchparameters)            {analyzeparameter (parameter); } Console.WriteLine ("return parameters of the parse method"); ParameterInfo Retpar=method.            Returnparameter;        Analyzeparameter (Retpar); }        /// <summary>        ///Analysis Method Parameters/// </summary>        /// <param name= "parameter" ></param>        Private Static voidanalyzeparameter (parameterinfo parameter) {Console.WriteLine ("parameter name:"+parameter.            Name); Console.WriteLine ("categories of parameters:"+parameter.            Attributes); Console.WriteLine ("type of parameter:"+parameter.        Parametertype.name); }        /// <summary>        ///Analysis Properties/// </summary>        /// <param name= "property" ></param>        Private Static voidAnalyzeproperty (PropertyInfo property) {Console.WriteLine ("attribute Name:"+Property .            Name); Console.WriteLine ("the category of the property:"+Property .            Attributes); Console.WriteLine ("whether it is readable:"+Property .            Canread.tostring ()); Console.WriteLine ("whether it can be written:"+Property .        Canwrite.tostring ()); }    }}

usingSystem;usingSystem.Reflection;usingSystem.Security.Permissions;namespaceusereflection{[PermissionSet (SecurityAction.Demand, Name="FullTrust")]    Partial classusereflection {Static voidMain (string[] args) {Assembly Assembly= Assembly.LoadFrom (@".. \.. \.. \.. \simpleassembly\simpleassembly\bin\debug\simpleassembly.exe");            analyzeassembly (assembly); //create an object of type in an assembly//Here you try to create a simpleassembly objectConsole.WriteLine ("creating types with Reflection"); string[] Pars = {"Test Reflection" }; Objecto = assembly. CreateInstance (assembly. GetModules () [0]. GetTypes () [0]. ToString (),true, Bindingflags.createinstance,NULL, Pars,NULL,NULL);            Console.WriteLine (o);        Console.read (); }    }}

"C #". NET what types are available to implement reflection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.