反射的关键作用在于降低项目的耦合度,其实就是对new的改进,在实例化一个对象的时候,可以完全不知道类本身的信息,这样就可以很灵活的对项目进行改进。
下面看一个例子:
首先我在D:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\反射\ClassLibrary1\bin\Debug\目录下创建了一个名为ClassLibrary1.dll的类库,实现了加和减的功能。如下:
using System;
using System.Collections.Generic;
using System.Text;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace ClassLibrary1
{
class Class1
{
/// <summary>
/// 加法
/// </summary>
/// <param name="a">第一个加数</param>
/// <param name="b">第二个加数</param>
/// <returns></returns>
public int add(int a, int b)
{
return a + b;
}
/// <summary>
/// 减法
/// </summary>
/// <param name="a">被减数</param>
/// <param name="b">减数</param>
/// <returns></returns>
public int jian(int a, int b)
{
return a - b;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
下面我用反射的方式实例化对象并调用方法:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;//实现反射的命名空间
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace 反射
{
class Program
{
static void Main(string[] args)
{
Assembly a = Assembly.LoadFrom(@"D:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\反射\ClassLibrary1\bin\Debug\ClassLibrary1.dll");//类库的存储路径
Type type = a.GetType("ClassLibrary1.Class1");//此处必须为类的完整名称
Object o = Activator.CreateInstance(type);//实例化类
MethodInfo method_add = type.GetMethod("add");//得到方法的信息
int i = (int)method_add.Invoke(o, new object[] { 1,2});//实现方法
Console.WriteLine(i);
}
}
}
可以看到,类的一些信息都是作为字符串的方式传入的,这就大大增加了程序的灵活性,可以通过配置文件的方式进行调用。反射的性能要低一些,实际中要合理应用。
下面看一个例子:
首先我在D:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\反射\ClassLibrary1\bin\Debug\目录下创建了一个名为ClassLibrary1.dll的类库,实现了加和减的功能。如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
下面我用反射的方式实例化对象并调用方法:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
肩负责任,永不退缩