コード例 #1
0
        /// <summary>
        /// 返回将对象从 <see cref="OriginType"/> 类型转换为 <paramref name="outputType"/> 类型的类型转换器。
        /// </summary>
        /// <param name="outputType">要将输入对象转换到的类型。</param>
        /// <returns>将对象从 <see cref="OriginType"/> 类型转换为 <paramref name="outputType"/>
        /// 类型的类型转换器,如果不存在则为 <c>null</c>。</returns>
        /// <remarks>返回的委托必须符合 <see cref="Converter{TInput,TOutput}"/>,
        /// 其输入类型是 <see cref="OriginType"/>,输出类型是 <paramref name="outputType"/>。</remarks>
        public Delegate GetConverterTo(Type outputType)
        {
            if (outputType.IsEnum)
            {
                return(DelegateBuilder.CreateDelegate(Convert.GetConverterType(typeof(string), outputType), convertToEnumMethod));
            }
            switch (Type.GetTypeCode(outputType))
            {
            case TypeCode.Boolean:
                return(new Converter <string, bool>(bool.Parse));

            case TypeCode.Char:
                return(new Converter <string, char>(char.Parse));

            case TypeCode.SByte:
                return(new Converter <string, sbyte>(sbyte.Parse));

            case TypeCode.Byte:
                return(new Converter <string, byte>(byte.Parse));

            case TypeCode.Int16:
                return(new Converter <string, short>(short.Parse));

            case TypeCode.UInt16:
                return(new Converter <string, ushort>(ushort.Parse));

            case TypeCode.Int32:
                return(new Converter <string, int>(int.Parse));

            case TypeCode.UInt32:
                return(new Converter <string, uint>(uint.Parse));

            case TypeCode.Int64:
                return(new Converter <string, long>(long.Parse));

            case TypeCode.UInt64:
                return(new Converter <string, ulong>(ulong.Parse));

            case TypeCode.Single:
                return(new Converter <string, float>(float.Parse));

            case TypeCode.Double:
                return(new Converter <string, double>(double.Parse));

            case TypeCode.Decimal:
                return(new Converter <string, decimal>(decimal.Parse));

            case TypeCode.DateTime:
                return(new Converter <string, DateTime>(DateTime.Parse));

            case TypeCode.DBNull:
                return(null);
            }
            return(null);
        }
コード例 #2
0
 /// <summary>
 /// 返回将对象从 <paramref name="inputType"/> 类型转换为 <see cref="OriginType"/> 类型的类型转换器。
 /// </summary>
 /// <param name="inputType">输入对象的类型。</param>
 /// <returns>将对象从 <paramref name="inputType"/> 类型转换为 <see cref="OriginType"/>
 /// 类型的类型转换器,如果不存在则为 <c>null</c>。</returns>
 /// <remarks>返回的委托必须符合 <see cref="Converter{TInput,TOutput}"/>,
 /// 其输入类型是 <paramref name="inputType"/>,输出类型是 <see cref="OriginType"/>。</remarks>
 public Delegate GetConverterFrom(Type inputType)
 {
     return(DelegateBuilder.CreateDelegate(Convert.GetConverterType(inputType, typeof(string)), convertToStringMethod));
 }
コード例 #3
0
        /// <summary>
        /// 返回与指定标识符相关的处理器方法集合。
        /// </summary>
        /// <typeparam name="TDelegate">使用基类型调用方法的委托。</typeparam>
        /// <param name="type">在其中查找静态或实例方法的类型。</param>
        /// <param name="id">方法切换器的标识符。</param>
        /// <param name="index">方法的关键参数索引。</param>
        /// <param name="queryStatic">是否请求的是静态方法。</param>
        /// <returns>与指定标识符相关的处理器方法集合。</returns>
        private static Dictionary <Type, Delegate> GetMethods <TDelegate>(Type type, string id, int index, bool queryStatic)
        {
            Type dlgType = typeof(TDelegate);
            Tuple <bool, Type, Dictionary <Type, Delegate> > data;
            string key = string.Concat(type.FullName, "_", id);

            if (!methodDict.TryGetValue(key, out data))
            {
                MethodInfo[]      methods = type.GetMethods(MethodFlags);
                List <MethodInfo> list    = new List <MethodInfo>();
                for (int i = 0; i < methods.Length; i++)
                {
                    if (methods[i].GetCustomAttributes(typeof(ProcessorAttribute), true)
                        .Cast <ProcessorAttribute>().Any(s => s.Id == id))
                    {
                        list.Add(methods[i]);
                    }
                }
                int cnt = list.Count;
                if (cnt == 0)
                {
                    throw CommonExceptions.ProcessorNotFound("type", type, id);
                }
                bool isStatic = list[0].IsStatic;
                for (int i = 1; i < cnt; i++)
                {
                    if (list[i].IsStatic != isStatic)
                    {
                        throw CommonExceptions.ProcessorMixed("type", type, id);
                    }
                }
                Dictionary <Type, Delegate> dict = new Dictionary <Type, Delegate>();
                Type newDlgType = dlgType;
                if (!isStatic)
                {
                    newDlgType = GetInstanceDlgType(newDlgType);
                }
                for (int i = 0; i < cnt; i++)
                {
                    Type     keyType = list[i].GetParameters()[index].ParameterType;
                    Delegate dlg     = DelegateBuilder.CreateDelegate(newDlgType, list[i], false);
                    if (dlg == null)
                    {
                        throw CommonExceptions.DelegateCompatible(list[i].ToString(), dlgType);
                    }
                    dict.Add(keyType, dlg);
                }
                data = new Tuple <bool, Type, Dictionary <Type, Delegate> >(isStatic, dlgType, dict);
                methodDict.Add(key, data);
            }
            if (data.Item1 != queryStatic)
            {
                throw CommonExceptions.ProcessorMismatch("type", type, id);
            }
            if (data.Item2 != dlgType)
            {
                // 检查委托参数。
                ParameterInfo[] paramInfos    = data.Item2.GetMethod("Invoke").GetParameters();
                ParameterInfo[] dlgParamInfos = dlgType.GetMethod("Invoke").GetParameters();
                if (paramInfos.Length != dlgParamInfos.Length)
                {
                    throw CommonExceptions.DelegateCompatible("TDelegate", dlgType);
                }
                for (int i = 0; i < paramInfos.Length; i++)
                {
                    if (paramInfos[i].ParameterType != dlgParamInfos[i].ParameterType)
                    {
                        throw CommonExceptions.DelegateCompatible("TDelegate", dlgType);
                    }
                }
            }
            return(data.Item3);
        }