程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> What is reflection and how to use it in CSharp ?

What is reflection and how to use it in CSharp ?

編輯:C#入門知識

/*

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


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved