(1)namespace ClassLibrarySport { public abstract class Sport { protected string name; public abstract string GetName(); public abstract string GetDuration(); } } = = = = = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = = (2)namespace ClassLibrarySomeSports//该项目添加了对(1)的引用 { public class Football : ClassLibrarySport.Sport { public Football() { name = “Football”; } public override string GetName() { return name; } public override string GetDuration() { return “four 15 minute quarters”; } } } = = = = = == = == = == = == = == = == = == = == = == = == = == = == = == = == = == = = (3)namespace ConsoleAssemblyTest//该项目添加了对(1)的引用 { class Program { static void Main(string[] args) { Assembly assembly = Assembly.LoadFrom(@”E:\ClassLibrarySomeSports\ bin\Debug\ClassLibrarySomeSports.dll”); Type[] types = assembly.GetTypes(); Console.WriteLine(“Get Type From ClassLibrarySomeSports.dll:”); for (int i = 0; i < types.Length; i++) { Console.WriteLine(types[i].Name); } //使用GetConstructor()方法获取对应类型的构造器,从而构造出该类型的对象 Console.WriteLine(“Use Method GetConstructor():”); ConstructorInfo ci = types[0].GetConstructor(new Type[0]); ClassLibrarySport.Sport sport = (ClassLibrarySport.Sport)ci.Invoke(new object[0]); Console.WriteLine(sport.GetName() + ” has ” + sport.GetDuration()); //使用Activator.CreateInstance()方法构造出该类型的对象 //使用assembly.CreateInstance()返回为null,?? Console.WriteLine(“Use Method CreateInstance():”); ClassLibrarySport.Sport sport1 = (ClassLibrarySport.Sport) Activator.CreateInstance(types[0]); Console.WriteLine(sport1.GetName() + ” has ” + sport1.GetDuration()); //反射指定类型中的名称为“GetDuration”的方法,通过Invoke()方法执行该方法 object objSport = Activator.CreateInstance(types[0]); MethodInfo method = types[0].GetMethod(“GetDuration”); object o = method.Invoke(objSport, new object[0]); Console.WriteLine(o as string); Console.Read(); } } } = = = = = == = == = == = == = == = == = == = == = == = == = == = == Output: Get Type From ClassLibrarySomeSports.dll: Football Use Method GetConstructor(): Football has four 15 minute quarters Use Method CreateInstance(): Football has four 15 minute quarters four 15 minute quarters http://www.cnblogs.com/KissKnife/archive/2006/08/13/475717.html
.net反射的简单示例_asp.net技巧
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » .net反射的简单示例_asp.net技巧
相关推荐
-      对.net framework 反射的反思_asp.net技巧
-      .net3.5和vs2008中的asp.net ajax_asp.net技巧
-      使用asp.net ajax框架扩展html map控件_asp.net技巧
-      asp.net应用程序资源访问安全模型_asp.net技巧
-      photoshop初学者轻松绘制螺旋漩涡特效_photoshop教程
-      photoshop通道结合图层模式抠狗尾巴草_photoshop教程
-      web.config详解+asp.net优化_asp.net技巧
-      asp.net中多彩下拉框的实现_asp.net技巧