日期:2014-05-18 浏览次数:20874 次
using System; using System.Reflection; public class AssemblyDemo { public static void Main(string[] args) { int i,j; if (args.GetLength(0) < 1) { Console.WriteLine("usage is AssemblyDemo < library_name >"); } else { Assembly assembly = Assembly.LoadFrom(args[0]); Type[] types = assembly.GetTypes(); Console.WriteLine(assembly.GetName().Name + " contains the following types"); for (i = 0 ; i < types.GetLength(0); ++i) { Console.WriteLine("\r(" + i + ") " + types[i].Name); } i = types.Length - 1; Console.Write("make selection(0-" + i + "):"); j = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(); if (types[j].IsSubclassOf(typeof(Sport))) { ConstructorInfo ci = types[j].GetConstructor(new Type[0]); Sport sport = (Sport)ci.Invoke(new Object[0]); Console.WriteLine(sport.GetName() + " has " + sport.GetDuration()); } else { Console.WriteLine(types[j].Name + " is not a sub-class of Sport"); } } } }
ConstructorInfo ci = types[j].GetConstructor(new Type[0]); Sport sport = (Sport)ci.Invoke(new Object[0]);