Beispiel #1
0
        private static ICbValue _CbCreate(Type type, string name, bool isCancelClass)
        {
            string typeName = type.Name;

            if (!isCancelClass && type.IsByRef)
            {
                // リファレンス情報を消す

                typeName = typeName.Replace("&", "");
            }

            switch (typeName)
            {
            case nameof(Byte): return(CbByte.Create(name));

            case nameof(SByte): return(CbSByte.Create(name));

            case nameof(Int16): return(CbShort.Create(name));

            case nameof(Int32): return(CbInt.Create(name));

            case nameof(Int64): return(CbLong.Create(name));

            case nameof(UInt16): return(CbUShort.Create(name));

            case nameof(UInt32): return(CbUInt.Create(name));

            case nameof(UInt64): return(CbULong.Create(name));

            case nameof(Char): return(CbChar.Create(name));

            case nameof(Single): return(CbFloat.Create(name));

            case nameof(Double): return(CbDouble.Create(name));

            case nameof(Decimal): return(CbDecimal.Create(name));

            case nameof(Boolean): return(CbBool.Create(name));

            case nameof(String): return(CbString.Create(name));

            case nameof(Object): return(CbObject.Create(name));

            case nameof(CbText): return(CbText.Create(name));

            case nameof(CbImagePath): return(CbImagePath.Create(name));

            default:
                break;
            }

            if (type.IsArray)
            {
                // 配列は、List<>に置き換える

                string _mame = "xxx";
                if (type.FullName != null)
                {
                    _mame = type.FullName;
                }
                else if (type.Name != null)
                {
                    _mame = type.Name;
                }
                Type tType = CbST.GetTypeEx(_mame);
                if (tType != null)
                {
                    Type element = tType.GetElementType();
                    if (element != null)
                    {
                        Type collectionType = typeof(List <>).MakeGenericType(element);
                        var  ret            = CbList.Create(collectionType, name);
                        if (ret.IsList)
                        {
                            ICbList cbList = ret.GetListValue;
                            cbList.IsArrayType = true;
                        }
                        return(ret);
                    }
                }
                if (type.ContainsGenericParameters)
                {
                    return(CbGeneMethArg.NTF(name, type, type.GetGenericArguments(), false));
                }
            }

            if (type.IsEnum)
            {
                // 列挙型

                return(CbEnumTools.EnumValue(type, name));
            }

            if (!type.IsGenericType && CbSTUtils.IsDelegate(type))
            {
                // デリゲート型

                return(CbFunc.FuncValue(type, typeof(CbVoid), name));
            }

            if (CbSTUtils.HaveGenericParamater(type))
            {
                // 確定していない型なので仮の型に差し替える

                if (!type.IsPublic)
                {
                    return(null);
                }

                if (CbSTUtils.IsDelegate(type))
                {
                    // 確定していないデリゲート型

                    return(CbGeneMethArg.NTF(name, type, type.GetGenericArguments(), true));
                }
                return(CbGeneMethArg.NTF(name, type, type.GetGenericArguments(), false));
            }

            if (type.IsGenericType)
            {
                // ジェネリック型

                if (CbList.HaveInterface(type, typeof(IEnumerable <>)))
                {
                    // リスト管理(UIで特別扱い)

                    if (type.GenericTypeArguments.Length > 1)
                    {
                        return(null);
                    }

                    return(CbList.Create(type, name));
                }

                if (CbSTUtils.IsDelegate(type))
                {
                    // デリゲート型

                    var returnType = CbSTUtils.GetDelegateReturnType(type);
                    if (CbSTUtils.IsVoid(returnType))
                    {
                        return(CbFunc.FuncValue(type, typeof(CbVoid), name));
                    }
                    else
                    {
                        return(CbFunc.FuncValue(type, returnType, name));
                    }
                }

                if (type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    // Null許容型

                    if (type.GenericTypeArguments.Length > 1)
                    {
                        return(null);
                    }

                    Type param = type.GenericTypeArguments[0];
                    switch (param.Name)
                    {
                    case nameof(Byte): return(CbNullableByte.Create(name));

                    case nameof(SByte): return(CbNullableSByte.Create(name));

                    case nameof(Int16): return(CbNullableShort.Create(name));

                    case nameof(Int32): return(CbNullableInt.Create(name));

                    case nameof(Int64): return(CbNullableLong.Create(name));

                    case nameof(UInt16): return(CbNullableUShort.Create(name));

                    case nameof(UInt32): return(CbNullableUInt.Create(name));

                    case nameof(UInt64): return(CbNullableULong.Create(name));

                    case nameof(Char): return(CbNullableChar.Create(name));

                    case nameof(Single): return(CbNullableFloat.Create(name));

                    case nameof(Double): return(CbNullableDouble.Create(name));

                    case nameof(Decimal): return(CbNullableDecimal.Create(name));

                    case nameof(Boolean): return(CbNullableBool.Create(name));

                    default:
                        break;
                    }
                    if (CbStruct.IsStruct(param))
                    {
                        // 構造体

                        return(CbStruct.NullableStructValue(param, name));
                    }
                    if (param.IsEnum)
                    {
                        // 列挙型

                        return(CbEnumTools.NullableEnumValue(param, name));
                    }
                    Debug.Assert(false);
                    return(null);
                }

                // その他のジェネリックは、構造体かクラスとして扱う
                if (CbStruct.IsStruct(type))
                {
                    return(CbStruct.StructValue(type, name));
                }
                return(CbClass.ClassValue(type, name));
            }

            if (CbStruct.IsStruct(type))
            {
                // 構造体

                if (type.FullName == "System.Void")
                {
                    Debug.Assert(false);
                    return(null);
                }

                if (!isCancelClass)
                {
                    var ret = CbStruct.StructValue(type, name);
                    if (ret != null && ret is ICbStruct cbStruct)
                    {
                        return(_CbCreate(cbStruct.OriginalReturnType, name, true));
                    }
                }
                return(CbStruct.StructValue(type, name));
            }

            if (type.IsClass || type.IsInterface)
            {
                // クラス

                var elements = CbSTUtils.GetGenericIEnumerables(type);
                if (elements.Count() != 0)
                {
                    // IEnumerable<> を持っているのでリストとして扱う(ただし、オリジナルのデータ形式も保存する)

                    var elementType = elements.First();   // 最初に見つかった要素のみを対象とする(妥協)
                    var chgType     = CbList.Create(typeof(IEnumerable <>).MakeGenericType(new Type[] { elementType }), name);
                    (chgType as ICbList).CastType = type; // 元の型にキャスト(メソッドを呼ぶときは、オリジナルのデータ形式を参照する)
                    return(chgType);
                }

                if (!isCancelClass)
                {
                    var ret = CbClass.ClassValue(type, name);
                    if (ret != null && ret is ICbClass cbClass)
                    {
                        return(_CbCreate(cbClass.OriginalReturnType, name, true));
                    }
                }
                return(CbClass.ClassValue(type, name));
            }

            return(null);
        }
Beispiel #2
0
        /// <summary>
        /// 対応するCbXXX型を求めます。
        /// </summary>
        /// <param name="type">オリジナルの型情報</param>
        /// <returns>CbXXX型の型情報</returns>
        public static Type ConvertCbType(Type type)
        {
            switch (type.Name)
            {
            case nameof(Byte): return(typeof(CbByte));

            case nameof(SByte): return(typeof(CbSByte));

            case nameof(Int16): return(typeof(CbShort));

            case nameof(Int32): return(typeof(CbInt));

            case nameof(Int64): return(typeof(CbLong));

            case nameof(UInt16): return(typeof(CbUShort));

            case nameof(UInt32): return(typeof(CbUInt));

            case nameof(UInt64): return(typeof(CbULong));

            case nameof(Char): return(typeof(CbChar));

            case nameof(Single): return(typeof(CbFloat));

            case nameof(Double): return(typeof(CbDouble));

            case nameof(Decimal): return(typeof(CbDecimal));

            case nameof(Boolean): return(typeof(CbBool));

            case nameof(String): return(typeof(CbString));

            case nameof(Object): return(typeof(CbObject));

            case nameof(CbText): return(typeof(CbText));

            case nameof(CbImagePath): return(typeof(CbImagePath));

            default:
                break;
            }

            if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                Type param = type.GenericTypeArguments[0];
                switch (param.Name)
                {
                case nameof(Byte): return(typeof(CbNullableByte));

                case nameof(SByte): return(typeof(CbNullableSByte));

                case nameof(Int16): return(typeof(CbNullableShort));

                case nameof(Int32): return(typeof(CbNullableInt));

                case nameof(Int64): return(typeof(CbNullableLong));

                case nameof(UInt16): return(typeof(CbNullableUShort));

                case nameof(UInt32): return(typeof(CbNullableUInt));

                case nameof(UInt64): return(typeof(CbNullableULong));

                case nameof(Char): return(typeof(CbNullableChar));

                case nameof(Single): return(typeof(CbNullableFloat));

                case nameof(Double): return(typeof(CbNullableDouble));

                case nameof(Decimal): return(typeof(CbNullableDecimal));

                case nameof(Boolean): return(typeof(CbNullableBool));

                default:
                    break;
                }
                if (CbStruct.IsStruct(param))
                {
                    // 構造体

                    Type cbStructType = typeof(CbNullableStruct <>).MakeGenericType(param);
                    return(cbStructType);
                }
                if (param.IsEnum)
                {
                    // 列挙型

                    Type cbEnumType = typeof(CbNullableEnum <>).MakeGenericType(param);
                    return(cbEnumType);
                }
                Debug.Assert(false);
                return(null);
            }

            if (type.IsEnum)
            {
                Type openedType = typeof(CbEnum <>);
                Type cbEnumType = openedType.MakeGenericType(type);
                return(cbEnumType);
            }

            if (CbStruct.IsStruct(type))
            {
                Type openedType   = typeof(CbStruct <>);
                Type cbStructType = openedType.MakeGenericType(type);
                return(cbStructType);
            }

            if (type.IsValueType)
            {
                // IsEnumとIsStructを抜けてきた未知の値型(今の所、対応する予定は無い)
                // IntPtr 型など

                return(null);
            }

            if (type.IsClass || type.IsInterface)
            {
                Type openedType  = typeof(CbClass <>);
                Type cbClassType = openedType.MakeGenericType(type);
                return(cbClassType);
            }

            if (type.GetGenericTypeDefinition() == typeof(ICollection <>))
            {
                Debug.Assert(false);
            }

            if (CbSTUtils.IsDelegate(type))
            {
                var returnType = CbSTUtils.GetDelegateReturnType(type);
                if (CbSTUtils.IsVoid(returnType))
                {
                    return(CbFunc.GetFuncType(type, typeof(CbVoid)));
                }
                else
                {
                    return(CbFunc.GetFuncType(type, returnType));
                }
            }

            return(null);
        }