动态string转换类

主要用于反射运行时转换类型

  class ConvertString
    {
        private static Dictionary<Type, DynamicMethodExecutor> executorCache = new Dictionary<Type, DynamicMethodExecutor>();
        /// <summary>
        /// Type必须有静态Parse方法供转换调用
        /// </summary>
        /// <param name="value"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static object ToType(string value, Type type)
        {
            if (type == typeof(string)) { return value; }
            DynamicMethodExecutor executor = null;
            if (executorCache.TryGetValue(type, out executor) == false)
            {
                MethodInfo parseMethod = null;
                foreach (MethodInfo mi in type.GetMethods(BindingFlags.Static | BindingFlags.Public))
                {
                    if (mi.Name == "Parse" && mi.GetParameters().Length == 1 && mi.GetParameters()[0].ParameterType == typeof(string))
                    {
                        parseMethod = mi; break;
                    }
                }
                if (parseMethod == null)
                {
                    throw new ArgumentException(string.Format("Type: {0} has not Parse static method!", type));
                }
                executor = new DynamicMethodExecutor(parseMethod);
                executorCache[type] = executor;
            }
            return executor.Execute(null, new object[] { value });
        }


        private class DynamicMethodExecutor
        {
            private Func<object, object[], object> m_execute;

            public DynamicMethodExecutor(MethodInfo methodInfo)
            {
                this.m_execute = this.GetExecuteDelegate(methodInfo);
            }

            public object Execute(object instance, object[] parameters)
            {
                return this.m_execute(instance, parameters);
            }

            private Func<object, object[], object> GetExecuteDelegate(MethodInfo methodInfo)
            {
                // parameters to execute
                ParameterExpression instanceParameter =
                    Expression.Parameter(typeof(object), "instance");
                ParameterExpression parametersParameter =
                    Expression.Parameter(typeof(object[]), "parameters");

                // build parameter list
                List<Expression> parameterExpressions = new List<Expression>();
                ParameterInfo[] paramInfos = methodInfo.GetParameters();
                for (int i = 0; i < paramInfos.Length; i++)
                {
                    // (Ti)parameters[i]
                    BinaryExpression valueObj = Expression.ArrayIndex(
                        parametersParameter, Expression.Constant(i));
                    UnaryExpression valueCast = Expression.Convert(
                        valueObj, paramInfos[i].ParameterType);

                    parameterExpressions.Add(valueCast);
                }

                // non-instance for static method, or ((TInstance)instance)
                Expression instanceCast = methodInfo.IsStatic ? null :
                    Expression.Convert(instanceParameter, methodInfo.ReflectedType);


                // static invoke or ((TInstance)instance).Method
                MethodCallExpression methodCall = Expression.Call(
                    instanceCast, methodInfo, parameterExpressions);

                // ((TInstance)instance).Method((T0)parameters[0], (T1)parameters[1], ...)
                if (methodCall.Type == typeof(void))
                {
                    Expression<Action<object, object[]>> lambda =
                        Expression.Lambda<Action<object, object[]>>(
                            methodCall, instanceParameter, parametersParameter);

                    Action<object, object[]> execute = lambda.Compile();
                    return (instance, parameters) =>
                    {
                        execute(instance, parameters);
                        return null;
                    };
                }
                else
                {
                    UnaryExpression castMethodCall = Expression.Convert(
                        methodCall, typeof(object));
                    Expression<Func<object, object[], object>> lambda =
                        Expression.Lambda<Func<object, object[], object>>(
                            castMethodCall, instanceParameter, parametersParameter);

                    return lambda.Compile();
                }
            }
        }
    }

 

转载于:https://www.cnblogs.com/FlyCat/archive/2012/08/27/2659335.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值