日期:2014-05-16 浏览次数:20883 次
private void WriteReflectionInfo() { Type testType = typeof(Test); Assembly assembly = testType.Assembly; Response.Write("Assembly:" + assembly.FullName + "<br/>"); Type[] typeList = assembly.GetTypes(); // 获取类型 // 针对每个类型获取详细信息 foreach (Type type in typeList) { Response.Write("------------------------" + type.Namespace + type.Name + "------------------------<br/>"); // 获得类型的结构信息 ConstructorInfo[] constructs = type.GetConstructors(); // 获得类型的字段信息 FieldInfo[] fields = type.GetFields(); Response.Write("<b>类的公共字段信息如下:</b>" + "<br/>"); int a1 = 1; foreach (FieldInfo field in fields) { Response.Write((a1++).ToString() + ". " + field.Name + "<br/>"); } // 获得方法信息 MethodInfo[] methods = type.GetMethods(); Response.Write("<b>类的公共方法如下:</b>" + "<br/>"); int a2 = 1; foreach (MethodInfo method in methods) { ParameterInfo[] parameters = method.GetParameters(); ParameterInfo reparam = method.ReturnParameter; Response.Write((a2++).ToString() + ". " + reparam.ParameterType.Name + " " + method.Name + "("); int index = 0; foreach (ParameterInfo para in parameters) { if (index++ < parameters.Length - 1) Response.Write(para.ParameterType.Name + " " + para.Name + ","); else Response.Write(para.ParameterType.Name + " " + para.Name); } Response.Write(")<br/>"); } // 获得属性的信息 PropertyInfo[] propertys = type.GetProperties(); Response.Write("<b>类的公共属性如下:</b>" + "<br/>"); int a3 = 1; foreach (PropertyInfo pro in propertys) { Response.Write((a3++).ToString() + ". " + pro.PropertyType.Name + " " + pro.Name + "{"); if (pro.CanRead) Response.Write("get;"); if (pro.CanWrite) Response.Write("set;"); Response.Write("}<br/>"); } // 获得事件信息 EventInfo[] events = type.GetEvents(); Response.Write("<b>类的成员如下:</b>" + "<br/>"); // 获得成员 int a4 = 1; foreach (MemberInfo mi in type.GetMembers()) { Response.Write((a4++).ToString() + ". " + mi.MemberType.ToString() + " : " + mi.Name + "<br/>"); } }
private void Assembly_CreateInstance() { string assemblyName = "SqlModel"; string className = assemblyName + ".Member"; // 创建无参数实例 IDAL.IMember member = (IDAL.IMember)Assembly.Load(assemblyName).CreateInstance(className); Response.Write("创建无参数实例:" + member.ID + "<br/>"); // 创建有参数实例 Object[] parameters = new Object[1]; parameters[0] = 10000; IDAL.IMember member1 = (IDAL.IMember)Assembly.Load(assemblyName).CreateInstance(className, false, BindingFlags.Default, null, parameters, null, null); Response.Write("创建有参数实例:" + member1.ID + "<br/>"); }