C # reflection Summary

Source: Internet
Author: User

Reflection, which is translated into Reflection in Chinese. This is.. Net ,. net applications are composed of several parts: 'assembly ', 'module', and 'Type (class) '. Reflection provides a programming method, this allows programmers to obtain relevant information about these components during the running period. For example, the Assembly class can obtain information about the running accessories and dynamically load the accessories, find the type information in the accessories and create an instance of this type. The Type class can obtain the Type information of an object. This information includes all elements of the object, such as methods, constructors, and attributes. The Type class can obtain and call the information of these elements. MethodInfo contains information about the method. You can use this class to obtain the name, parameter, and return value of the method and call it. For example, FieldInfo and EventInfo are included in the System. Reflection namespace.
I. Type is used to obtain Type information.
The System. Type class plays a core role in reflection. When a reflection request loads a Type, the common language runtime creates a Type for it. You can use the method, field, attribute, and nested class of the Type object to find all information about the Type.
You can clearly understand the Type by running the Pipeline Code and analyzing the result.

 

Obtain type information
Namespace ConsoleApplication2
{
Class Program
{
Static void Main (string [] args)
{
MyClass m = new MyClass ();
Type type = m. GetType ();
Console. WriteLine ("type Name:" + type. Name );
Console. WriteLine ("class Full name:" + type. FullName );
Console. WriteLine ("Namespace name:" + type. Namespace );
Console. WriteLine ("collection name:" + type. Assembly );
Console. WriteLine ("Module name:" + type. Module );
Console. WriteLine ("base class name:" + type. BaseType );
Console. WriteLine ("whether class:" + type. IsClass );
Console. WriteLine ("Public member of the class :");
MemberInfo [] memberInfos = type. GetMembers (); // get all public members
Foreach (var item in memberInfos)
{
Console. WriteLine ("{0 }:{ 1}", item. MemberType, item );
}
}

}
Class MyClass
{
Public string m;
Public void test ()
{}
Public int MyProperty {
Related Article

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.