Пример #1
0
        /// <summary>
        /// 得到过程委托
        /// </summary>
        /// <param name="method">方法对象</param>
        /// <returns>返回过程委托</returns>
        public static Proc GetProc(this System.Reflection.MethodInfo method)
        {
            Guard.NotNull(method, "method");

            var proc = method.DeclaringType.IsValueType
                 ? (target, args) => method.Invoke(target, args)
                 : DefaultDynamicMethodFactory.CreateProcMethod(method);


            return((target, args) =>
            {
                if (args == null)
                {
                    args = new object[method.GetParameters().Length];
                }

                try
                {
                    proc(target, args);
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            });
        }
Пример #2
0
        /// <summary>
        /// 得到构造函数委托
        /// </summary>
        /// <param name="constructor">构造函数</param>
        /// <returns>返回构造函数委托</returns>
        public static ConstructorHandler GetCreator(this System.Reflection.ConstructorInfo constructor)
        {
            Guard.NotNull(constructor, "constructor");

            ConstructorHandler ctor = constructor.DeclaringType.IsValueType ?
                                      (args) => constructor.Invoke(args)
                : DefaultDynamicMethodFactory.CreateConstructorMethod(constructor);

            ConstructorHandler handler = args =>
            {
                if (args == null)
                {
                    args = new object[constructor.GetParameters().Length];
                }
                try
                {
                    return(ctor(args));
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            };

            return(handler);
        }
Пример #3
0
        /// <summary>
        /// 得到设置器委托
        /// </summary>
        /// <param name="member">成员</param>
        /// <returns>返回设置器委托</returns>
        public static Setter GetSetter(this System.Reflection.MemberInfo member)
        {
            Guard.NotNull(member, "member");

            Setter setter = null;

            if (member.DeclaringType.IsValueType)
            {
                switch (member.MemberType)
                {
                case MemberTypes.Field:
                    setter = (target, value) => (member as FieldInfo).SetValue(target, value);
                    break;

                case MemberTypes.Property:
                    setter = (target, value) => (member as PropertyInfo).SetValue(target, value, null);
                    break;

                case MemberTypes.Method:
                    setter = (target, value) => (member as MethodInfo).Invoke(target, new object[] { value });
                    break;
                }
            }
            else
            {
                setter = DefaultDynamicMethodFactory.CreateSetter(member);
            }

            return((target, value) =>
            {
                try
                {
                    if (setter != null)
                    {
                        setter(target, value);
                    }
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            });
        }
Пример #4
0
        /// <summary>
        /// 得到缺省构造函数委托
        /// </summary>
        /// <param name="binderType"></param>
        /// <returns></returns>
        public static DefaultConstructorHandler GetDefaultCreator(this Type type)
        {
            Guard.NotNull(type, "type");
            var ctor = DefaultDynamicMethodFactory.CreateDefaultConstructorMethod(type);

            DefaultConstructorHandler handler = () =>
            {
                try
                {
                    return(ctor());
                }
                catch (TargetInvocationException ex)
                {
                    throw ex.InnerException;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            };

            return(handler);
        }