(一)首先我们来创建一个用于反射的例子
1.我们来创建一个用于反射的类库 test.dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace test
{
public class ReflectTest
{
public string GetName(string name)
{
return "我的姓名:" + name;
}
public string GetAge(string age)
{
return "我的年龄:" + age;
}
}
}
2.我们来利用反射,和不利用反射来调用这个类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
#region 一般情况下我们的处理方式
test.ReflectTest reflectTest = new test.ReflectTest();
string ss = reflectTest.GetName("zhoumin");
Console.Write(ss);
Console.ReadLine();
#endregion
#region 反射机制处理
System.Reflection.Assembly ass;
Type type;
object obj;
//加载程序集
ass = System.Reflection.Assembly.LoadFile(@"d:\test.dll");
//得到某个类的类型信息
type = ass.GetType("test.ReflectTest");//必须使用命名空间+类名称
//创建类的实例
obj = ass.CreateInstance("test.ReflectTest");//必须使用名称空间+类名称
//得到方法信息
System.Reflection.MethodInfo method = type.GetMethod("GetName");//方法的名称
//实例方法的调用
string s = (string)method.Invoke(obj, new string[] { "zhoumin" });
Console.Write(s);
Console.ReadLine();
#endregion
}
}
}
上面是一个反射的例子;
有人肯定会问了。反射调用一个方法,和我用一个这个类的实例直接来调用结果一样,而且反射写的代码比较多。干嘛要有反射的存在呢?
以下是我个人的理解,如有疑问,请发表以下评论,大家可以相互学习
1.在写程序的时候,有时候,类库很大,里面的类很多,我只记得有一个类的名字,我就不需要添加引用,这要动态加载这个类库的dll文件,和这个类名,我就可以调用这个类了。(而且这是在运行期间动态的创建这个类的实例)上面我们平时写的例子,首先我得引用这个类,其实在编译的时候就告诉了程序调用这个类的地址。而反射是在运行的时候,动态的告诉程序调用这个类的地址;
2.晚绑定能够带来很多设计上的便利,合适的使用能够大大提高程序的复用性和灵活性
ex:
Type[] MyGrabSites = Assembly.LoadFile(Directory.GetParent(Assembly.GetEntryAssembly().Location)+"\\WorkingPlatforms_Bunisess.dll").GetTypes();
foreach (Type MySitetype in MyGrabSites)
{
//加载指定命名空间下符合ID的那个业务类
if (MySitetype.Namespace == bag.Business_NameSpace.Trim() && (!MySitetype.IsNestedPrivate))
{
object myintance = Activator.CreateInstance(MySitetype);
//该业务类须继承AbstractFactory 且被声明为公有
if (myintance is AbstractFactory)
{
AbstractFactory siteInstance = myintance as AbstractFactory;
if (siteInstance.ID == int.Parse(bag.CurrentSite.ID))
{
//如果业务ID相同则创建这个类的实例并调用之
Currentob = siteInstance;
siteInstance.GrbDate = bag.GrbDate;
siteInstance.PageCount = bag.PageCount;
siteInstance.PageNumber = bag.PageNumber;
siteInstance.StartPageNumber = bag.StartPageNumber;
siteInstance.EndPageNumber = bag.EndPageNumber;
siteInstance.CurrentSite = bag.CurrentSite;
siteInstance.IsGetHistory = bag.IsGetHistory;
siteInstance.IsDouble = bag.IsDouble;
siteInstance.OnWorkdoingCompleteEvent += new WorkingPlatEventHandle(siteInstance_OnWorkdoingCompleteEvent);
siteInstance.OnworkdoingEvent += new WorkingPlatEventHandle(siteInstance_OnworkdoingEvent);
siteInstance.OnErrorOccuringEvent += new WorkingPlatEventHandle(siteInstance_OnErrorOccuringEvent);
siteInstance.OnSaveCompletedEvent += new WorkingPlatEventHandle(siteInstance_OnSaveCompletedEvent);
if (GrabSite)
siteInstance.StartDoworking(bag.CurrentSite);
else
{
siteInstance.StartDoworking(bag);
}
break;
}
}
}
}
这样我就不需要去在其他地方来实例化这个类并调用之,我只需要加一个类(有个ID)我不需要new对象。便于我业务的添加,以及各个类之间的耦合性得到降低;-----10分