What is reflection and how to use it in CSharp?

Source: Internet
Author: User

/*

Author: Jiangong SUN

*/


Reflection provides objects (of type Type) that encapsulate assemblies, modules and types. you can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. if you are using attributes in your code, Reflection enables you to access them.

 

 

Here I want to demonstrate how to get a class 'assembly, module, method and attributes information.

 


Firstly, I 've created a class with fields, attributes, method and properties.

 


And then I 've created a new class named "RunBasicReflection" to get all the information I need.

 

 

 

Then, I get the type information using method "GetTypeInformation ".

 


Within the same "main" method, I'll create an instance of type "BasicReflection" and invoke the method "Calculation ".

 
 


And then, I'll get the type's assembly information.

First, call method "GetAssemblyInformation"

 


The content of method "GetAssemblyInformation"

 
 


And then, I'll get the information about module, method, attributes etc.

 

 
 

 

I 've spent some time to clarify all this information about reflection. I hope this post can do help in your daily work! Enjoy coding!

 


References:

Http://stackoverflow.com/questions/2712629/what-is-reflection-in-c-where-do-we-use-this-concept-in-our-application


Http://www.codeproject.com/Articles/38870/Examining-an-Assembly-at-Runtime


 

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.