Example #1
0
 public override Object Invoke(Object target, MethodBase method, params Object[] parameters)
 {
     if (method is MethodInfo)
     {
         return(MethodInfoX.Create(method).Invoke(target, parameters));
     }
     else
     {
         return(base.Invoke(target, method, parameters));
     }
 }
Example #2
0
 /// <summary>反射调用指定对象的方法</summary>
 /// <param name="target">要调用其方法的对象,如果要调用静态方法,则target是类型</param>
 /// <param name="method">方法</param>
 /// <param name="parameters">方法参数字典</param>
 /// <returns></returns>
 public override Object InvokeWithParams(Object target, MethodBase method, IDictionary parameters)
 {
     if (method is MethodInfo)
     {
         return(MethodInfoX.Create(method).InvokeWithParams(target, parameters));
     }
     else
     {
         return(base.Invoke(target, method, parameters));
     }
 }
Example #3
0
        /// <summary>通过传入参数字典快速调用静态方法</summary>
        /// <typeparam name="TTarget"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="name">名称</param>
        /// <param name="parameters">参数数组</param>
        /// <returns></returns>
        public static TResult InvokeWithParams <TTarget, TResult>(String name, IDictionary parameters)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            MethodInfoX mix = Create(typeof(TTarget), name);

            if (mix == null)
            {
                throw new XException("类{0}中无法找到{1}方法!", typeof(TTarget).Name, name);
            }

            return((TResult)mix.InvokeWithParams(null, parameters));
        }
Example #4
0
        /// <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));
        }
Example #5
0
        /// <summary>通过传入参数字典快速调用方法</summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="target">目标对象</param>
        /// <param name="name">名称</param>
        /// <param name="parameters">参数数组</param>
        /// <returns></returns>
        public static TResult InvokeWithParams <TResult>(Object target, String name, IDictionary parameters)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (String.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            MethodInfoX mix = Create(target.GetType(), name);

            if (mix == null)
            {
                throw new XException("类{0}中无法找到{1}方法!", target.GetType().Name, name);
            }

            return((TResult)mix.InvokeWithParams(target, parameters));
        }
Example #6
0
        /// <summary>创建快速访问成员</summary>
        /// <param name="member"></param>
        /// <returns></returns>
        public static MemberInfoX Create(MemberInfo member)
        {
            if (member == null)
            {
                return(null);
            }

            switch (member.MemberType)
            {
            case MemberTypes.All:
                break;

            case MemberTypes.Constructor:
                return(ConstructorInfoX.Create(member as ConstructorInfo));

            case MemberTypes.Custom:
                break;

            case MemberTypes.Event:
                return(EventInfoX.Create(member as EventInfo));

            case MemberTypes.Field:
                return(FieldInfoX.Create(member as FieldInfo));

            case MemberTypes.Method:
                return(MethodInfoX.Create(member as MethodInfo));

            case MemberTypes.Property:
                return(PropertyInfoX.Create(member as PropertyInfo));

            case MemberTypes.TypeInfo:
            case MemberTypes.NestedType:
                return(TypeX.Create(member as Type));

            default:
                break;
            }
            return(null);
        }
Example #7
0
        /// <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
        }