标签:
using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Reflection; namespace codeTest { class Program { static void Main(string[] args) { string str = "Hello"; //C#中基于System.Type的反射 Type t = str.GetType(); Console.WriteLine(t.FullName); Type t2 = Type.GetType("System.String", false, true); Console.WriteLine(t2.FullName); Type t3 = typeof(string); Console.WriteLine(t3.FullName); //C#中对于方法、属性等的反射 //GetMethods(t, BindingFlags.Public | BindingFlags.Instance); Console.WriteLine("{0}", t.GetMethod("Copy")); t.GetProperties(); t.GetFields(); //C#中动态加载以及推迟绑定 //获取程序集 Assembly assembly = Assembly.Load("codeTest"); //Assembly assembly2 = Assembly.LoadFrom("codeTest.dll"); //从程序集中获取指定对象类型; Type type = assembly.GetType("codeTest.User"); //使用Activator创建实例(无参数构造函数) var user1 = Activator.CreateInstance(type); //使用Activator创建实例(带参数构造函数) var user2 = Activator.CreateInstance(type, "薄暮"); //使用Assembly创建实例(无参数构造函数) var user3 = assembly.CreateInstance("codeTest.User"); //反射无参构造函数 ConstructorInfo constructor1 = type.GetConstructor(new Type[] { }); var user4 = constructor1.Invoke(new object[] { }); //反射有参构造函数 ConstructorInfo constructor2 = type.GetConstructor(new Type[] { typeof(string) }); var user5 = constructor2.Invoke(new object[] { "薄暮" }); //调用public函数(无参数) type.InvokeMember("PublicShow", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, user1, null); //调用public函数(带参数) string returnValue = type.InvokeMember("GetString", BindingFlags.InvokeMethod | BindingFlags.OptionalParamBinding, null, user1, new object[] { "薄暮" }) as string; // 调用静态方法 string returnValue2 = type.InvokeMember("StaticMethod", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, null, new object[] { }) as string; // 调用私有方法 . type.InvokeMember("PrivateShow", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, user1, new object[] { }); //反射属性 var Name = type.InvokeMember("Name", BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance, null, user1, new object[] { }) as string; //设置属性(设置Name属性为"新属性") type.InvokeMember("Name", BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance, null, user1, new object[] { "新属性" }); //反射字段 string Field = type.InvokeMember("Field", BindingFlags.GetField | BindingFlags.Public | BindingFlags.Instance, null, user1, new object[] { }) as string; //设置字段(设置Field字段为"新字段") type.InvokeMember("Field", BindingFlags.SetField | BindingFlags.Public | BindingFlags.Instance, null, user1, new object[] { "新字段" }); Console.ReadLine(); } public static void GetMethods(Type t, BindingFlags f) { MethodInfo[] info = t.GetMethods(f); foreach (var item in info) { Console.WriteLine("{0}", item.Name); } } } public class User { //字段 public string Field; //属性 public string Name { get; set; } //构造函数 public User() { this.Name = "无参构造"; } public User(string name) { this.Name = name; } //public函数 public void PublicShow() { Console.WriteLine(string.Format("反射调用一个public方法")); } //private函数 private void PrivateShow() { Console.WriteLine(string.Format("反射调用一个Private方法")); } //static函数 public static string StaticMethod() { return "反射调用了一个Static方法"; } //带参带返回值函数 public string GetString(string name) { return string.Format("大家好,我的名字是:{0}!", name); } //事件 public event EventHandler eventHandler; //事件处理函数 public void DoEvent() { if (eventHandler != null) eventHandler(null, EventArgs.Empty); } } }
标签:
原文地址:http://www.cnblogs.com/lgxlsm/p/4782694.html