首页 新闻 搜索 专区 学院

.NET反射技术

0
[已解决问题] 解决于 2012-06-13 17:15

.NET中的反射技术具体使用的类,具体应用的场景,具体关注的接口,现在没分了,有了一定追加!!!

感激不尽!

乔乔lovefreedom的主页 乔乔lovefreedom | 初学一级 | 园豆:9
提问于:2011-01-26 18:12
< >
分享
最佳答案
0

下面的例子说明如何动态加载assembly, 及创建实例,设置属性值:

// create instance of class DateTime

DateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime));

 

 

// create instance of DateTime, use constructor with parameters (year, month, day)

DateTime dateTime = (DateTime)Activator.CreateInstance(typeof(DateTime),

                                                       new object[] { 2008, 7, 4 });

 

 

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() { ... }

    }

}

 

 [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);

奖励园豆:5
拥有的都是恩典 | 菜鸟二级 |园豆:389 | 2011-01-27 06:43
谢谢
乔乔lovefreedom | 园豆:9 (初学一级) | 2011-02-13 12:52
其他回答(1)
0

MSDN一下。。。

zfy | 园豆:169 (初学一级) | 2011-01-26 20:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册