博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Reflection Examples [C#]
阅读量:5880 次
发布时间:2019-06-19

本文共 3259 字,大约阅读时间需要 10 分钟。

This example shows how to dynamically load assembly, how to create object instance, how to invoke method or how to get and set property value.

Create instance from assembly that is in your project References

The following examples create instances of DateTime class from the System assembly.

[C#]

// create instance of class DateTime DateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime));

[C#]

// create instance of DateTime, use constructor with parameters (year, month, day) DateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime), new object[] { 2008, 7, 4 });

Create instance from dynamically loaded assembly

All the following examples try to access to sample class Calculator from Test.dll assembly. The calculator class can be defined like this.

[C#]

namespace Test { public class Calculator { public Calculator() { ... } private double _number; public double Number { get { ... } set { ... } } public void Clear() { ... } private void DoClear() { ... } public double Add(double number) { ... } public static double Pi { ... } public static double GetPi() { ... } } }

Examples of using reflection to load the Test.dll assembly, to create instance of the Calculator class and to access its members (public/private, instance/static).

[C#]

// dynamically load assembly from file Test.dll Assembly testAssembly = Assembly.LoadFile(@"c:\Test.dll");

[C#]

// get type of class Calculator from just loaded assembly Type calcType = testAssembly.GetType("Test.Calculator");

[C#]

// create instance of class Calculator object calcInstance = Activator.CreateInstance(calcType);

[C#]

// get info about property: public double Number PropertyInfo numberPropertyInfo = calcType.GetProperty("Number");

[C#]

// get value of property: public double Number double value = (double)numberPropertyInfo.GetValue(calcInstance, null);

[C#]

// set value of property: public double Number numberPropertyInfo.SetValue(calcInstance, 10.0, null);

[C#]

// get info about static property: public static double Pi PropertyInfo piPropertyInfo = calcType.GetProperty("Pi");

[C#]

// get value of static property: public static double Pi double piValue = (double)piPropertyInfo.GetValue(null, null);

[C#]

// invoke public instance method: public void Clear() calcType.InvokeMember("Clear", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, calcInstance, null);

[C#]

// invoke private instance method: private void DoClear() calcType.InvokeMember("DoClear", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic, null, calcInstance, null);

[C#]

// invoke public instance method: public double Add(double number) double value = (double)calcType.InvokeMember("Add", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public, null, calcInstance, new object[] { 20.0 });

[C#]

// invoke public static method: public static double GetPi() double piValue = (double)calcType.InvokeMember("GetPi", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, null, null, null);

[C#]

// get value of private field: private double _number double value = (double)calcType.InvokeMember("_number", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic, null, calcInstance, null);

转载地址:http://lodix.baihongyu.com/

你可能感兴趣的文章
学习CodeIgniter框架之旅(二)继承自定义类
查看>>
Y2161 Hibernate第三次考试 2016年8月18日 试卷分析
查看>>
Angular CLI 使用教程指南参考
查看>>
PHP 程序员的技术成长规划
查看>>
用于守护进程的出错处理函数
查看>>
memcached 分布式聚类算法
查看>>
禁止body滚动允许div滚动防微信露底
查看>>
Xtreme8.0 - Kabloom dp
查看>>
jquery css3问卷答题卡翻页动画效果
查看>>
MDK5.00中*** error 65: access violation at 0xFFFFFFFC : no 'write' permission的一种解决方法
查看>>
Android 集成支付宝支付详解
查看>>
SQL分布式查询、跨数据库查询
查看>>
C#------连接SQLServer和MySQL字符串
查看>>
Arcgis Licensemanager 不能启动的原因之一(转载)
查看>>
(原)Android在子线程用handler发送的消息,主线程是怎么loop到的?
查看>>
$digest already in progress 解决办法——续
查看>>
虚拟机 centos设置代理上网
查看>>
Struts2中Date日期转换的问题
查看>>
mysql 数据类型
查看>>
Ubuntu 设置当前用户sudo免密码
查看>>