What is reflection and how to use it in CSharp ?

/*

Author: Jiangong SUN

*/


Last update: 15/07/2013


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.

With reflection, you can inspect the meta data.


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

http://stackoverflow.com/questions/4646786/dynamic-lang-runtime-vs-reflection

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值