虽说使用C#已经很长时间了,但是对反射的认识不是很清楚(惭愧!!),今天无事,特写了一段测试代码,以研究reflection的特性 using System; using System.Reflection; using System.Collections.Generic; using System.Linq; using System.Text; using RelectionDemo.Utility; using RelectionDemo.CustomeAttribute; namespace RelectionDemo { class Program { static void Main(string[] args) { TestExternalReflection(); //get the type of TestClass Type type = Type.GetType("RelectionDemo.Utility.TestClass", false); Console.WriteLine("Assembly:{0}", type.Assembly); Console.WriteLine("AssemblyQualifiedName:{0}", type.AssemblyQualifiedName); Console.WriteLine("Name:{0}", type.Name); Console.WriteLine("Namespace:{0}", type.Namespace); //create the instance of this type Console.WriteLine(); Console.WriteLine("the first way to create a instance by reflection"); //get constructor that takes no parameters ConstructorInfo ctorNoParams = type.GetConstructor(Type.EmptyTypes); //execute this constructor to create a class instance TestClass tcNoParams = ctorNoParams.Invoke(new object[0]) as TestClass; //test whether the instance is created successfully. if (tcNoParams == null) { throw new Exception("Can't create the instance by reflection"); } //get constructor that takes parameters ConstructorInfo ctorParams = type.GetConstructor(new Type[] { typeof(string), typeof(int) }); //execute this constructor to create a class instance TestClass tcParams = ctorParams.Invoke(new object[2]{"Bixiangyang",28}) as TestClass; //test whether the instance is created successfully. if (tcParams == null) { throw new Exception("Can't create the instance by reflection"); } //use InvokeMember to create a instance by call constructor that takes no parameters TestClass tcVokeNoParams = type.InvokeMember(null, BindingFlags.CreateInstance, null, null, new object[0]{}) as TestClass; if (tcVokeNoParams == null) { throw new Exception("Can't create the instance by reflection"); } //use InvokeMember to create a instance by call constructor that takes parameters TestClass tcVoke = type.InvokeMember(type.Name.ToString(), BindingFlags.CreateInstance, null, null, new object[] { "Wanghe", 16 }) as TestClass; if (tcVoke == null) { throw new Exception("Can't create the instance by reflection"); } //invoke a public method object ret; ret = type.InvokeMember("GetName", BindingFlags.InvokeMethod, null, tcVoke, null); ret = type.InvokeMember("SetAge", BindingFlags.InvokeMethod, null, tcVoke, new object[1] { 15 }); //invoke the static member type.InvokeMember("SetTheFirstObjectIndex", BindingFlags.InvokeMethod, null, tcVoke, new object[1] { 1 }); //get the static field ret = type.InvokeMember("ObjCnt", BindingFlags.GetField, null, null, new object[0] { }); //get the field that 's not static ret = type.InvokeMember("ObjNum", BindingFlags.GetField, null, tcVoke, new object[0] { }); //get TestClass all member FieldInfo[] publicFieldInfos = type.GetFields(BindingFlags.Public|BindingFlags.Instance); FieldInfo[] nonPublicFieldInfos = type.GetFields(BindingFlags.NonPublic|BindingFlags.Instance); FieldInfo[] allFieldInfos = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic|BindingFlags.Static); allFieldInfos = type.GetFields(); PropertyInfo[] propertyInfos = type.GetProperties(); //enumerate the attributes of property for (int i = 0; i < propertyInfos.Length; i++) { PropertyInfo property = propertyInfos[i]; var attr = property.GetCustomAttributes(typeof(CountryAttribute), false); if (attr == null) { } } EventInfo[] eventInfos = type.GetEvents(); MethodInfo[] methodInfos = type.GetMethods(); foreach(MethodInfo methodInfo in methodInfos) { //call generic method if (methodInfo.IsGenericMethod) { MethodInfo instanceMethod = methodInfo.MakeGenericMethod(typeof(string)); instanceMethod.Invoke(tcVoke, new object[1] { "JiaShuo" }); } } MemberInfo[] memberInfos = type.GetMembers(); //Generic Type Type genericType = Type.GetType("RelectionDemo.GenericType.EleManager"); if (genericType.IsGenericType) { Type instanceType = genericType.MakeGenericType(typeof(string)); } } static void TestExternalReflection() { //external class library //The assembly-qualified name of the type to get. See AssemblyQualifiedName. //If the type is in the currently executing assembly or in Mscorlib.dll, //it is sufficient to supply the type name qualified by its namespace. Type type = Type.GetType("DllProvider.ConcreteClass.Calculator,DllProvider"); int i = 0; } } }