/// <summary>快速调用静态方法</summary> /// <typeparam name="TTarget">目标类型</typeparam> /// <typeparam name="TResult">返回类型</typeparam> /// <param name="name">名称</param> /// <param name="parameters">参数数组</param> /// <returns></returns> public static TResult Invoke <TTarget, TResult>(String name, params Object[] parameters) { if (String.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } MethodInfoX mix = Create(typeof(TTarget), name, TypeX.GetTypeArray(parameters)); if (mix == null) { throw new XException("类{0}中无法找到{1}方法!", typeof(TTarget).Name, name); } return((TResult)mix.Invoke(null, parameters)); }
/// <summary></summary> public static void Test() { XTrace.WriteLine("创建类型……"); #region TypeX类型 TypeX type = TypeX.Create(typeof(FastTest)); Object obj = type.CreateInstance(); Debug.Assert(obj != null, "创建实例出错!"); obj = type.CreateInstance(123); Debug.Assert(obj != null, "创建实例出错!"); //obj = type.CreateInstance("1234"); //Debug.Assert(obj != null, "创建实例出错!"); obj = type.CreateInstance(111, "aaa"); Debug.Assert(obj != null, "创建实例出错!"); XTrace.WriteLine("创建值类型实例"); type = TypeX.Create(typeof(ConsoleKeyInfo)); obj = type.CreateInstance(); Debug.Assert(obj != null, "创建值类型实例出错!"); XTrace.WriteLine("创建数组类型实例"); type = TypeX.Create(typeof(ConsoleKeyInfo[])); obj = type.CreateInstance(5); Debug.Assert(obj != null, "创建数组类型实例出错!"); #endregion #region 构造函数 ConstructorInfoX ctr = ConstructorInfoX.Create(typeof(FastTest)); obj = ctr.CreateInstance(); Debug.Assert(obj != null, "创建实例出错!"); ctr = ConstructorInfoX.Create(typeof(FastTest), new Type[] { typeof(Int32) }); obj = ctr.CreateInstance(123); Debug.Assert(obj != null, "创建实例出错!"); ctr = ConstructorInfoX.Create(typeof(FastTest), new Type[] { typeof(Int32), typeof(String) }); obj = ctr.CreateInstance(111, "aaa"); Debug.Assert(obj != null, "创建实例出错!"); #endregion #region 字段 FieldInfoX field = FieldInfoX.Create(typeof(FastTest), "_ID"); (obj as FastTest).ID = 111; Int32 v = (Int32)field.GetValue(obj); Debug.Assert(v == 111, "字段取值出错!"); field.SetValue(obj, 888); v = (Int32)field.GetValue(obj); Debug.Assert(v == 888, "字段赋值出错!"); KeyValuePair <Int32, Int32> kv = new KeyValuePair <int, int>(123456, 222); field = FieldInfoX.Create(kv.GetType(), "Key"); //field.SetValue(kv, 123456); v = (Int32)field.GetValue(kv); Debug.Assert(v == 123456, "字段取值出错!"); field = FieldInfoX.Create(typeof(FastTest), "_Name"); field.SetValue("动态赋值"); String v2 = (String)field.GetValue(); Debug.Assert(v2 == "动态赋值", "静态字段出错!"); #endregion #region 属性 PropertyInfoX p = typeof(FastTest).GetProperty("ID"); v = (Int32)p.GetValue(obj); Debug.Assert(v == 888, "属性取值出错!"); p.SetValue(obj, 999); v = (Int32)p.GetValue(obj); Debug.Assert(v == 999, "属性赋值出错!"); p = PropertyInfoX.Create(typeof(FastTest), "Name"); field.SetValue("属性动态赋值"); v2 = (String)field.GetValue(); Debug.Assert(v2 == "属性动态赋值", "静态字段出错!"); #endregion #region 方法 MethodInfoX method = MethodInfoX.Create(typeof(FastTest), "Test2"); method.Invoke(obj); method = typeof(FastTest).GetMethod("GetFullName"); Console.WriteLine(method.Invoke(null, 123, "abc")); #endregion }