Exemplo n.º 1
0
        /// <summary> 构造用于拓展系统Type对象的属性和方法的对象
        /// </summary>
        /// <param name="type">用于构造TypeInfo的Type对象实例,不可为null</param>
        internal TypeInfo(System.Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            Type     = type;
            TypeCode = System.Type.GetTypeCode(type);
            IsArray  = type.IsArray;

            IsMakeGenericType = type.IsGenericType && !type.IsGenericTypeDefinition;
            var valueType = Nullable.GetUnderlyingType(type);

            if (valueType != null) //判断可空值类型
            {
                IsNullable             = true;
                NullableUnderlyingType = TypesHelper.GetTypeInfo(valueType);
                type = valueType;
            }

            if (type.IsEnum)
            {
                IsNumberType       = true;
                EnumUnderlyingType = TypesHelper.GetTypeInfo(Enum.GetUnderlyingType(Type));
            }
            else if (IsNullable)
            {
                IsNumberType = NullableUnderlyingType.IsNumberType;
            }
            else
            {
                IsNumberType = (TypeCode >= TypeCode.SByte && TypeCode <= TypeCode.Decimal);
            }
        }
Exemplo n.º 2
0
        /// <summary> 获取缓存
        /// </summary>
        /// <param name="type">反射对象类型</param>
        /// <param name="ignoreCase">属性/字段名称是否忽略大小写</param>
        /// <exception cref="ArgumentException">缓存中的对象类型与参数type不一致</exception>
        /// <exception cref="ArgumentNullException">参数type为null</exception>
        public static Literacy Cache(Type type, bool ignoreCase)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            var info = TypesHelper.GetTypeInfo(type);

            return(ignoreCase ? info.IgnoreCaseLiteracy : info.Literacy);
        }
Exemplo n.º 3
0
 /// <summary> 初始化
 /// </summary>
 private void Init()
 {
     Name      = MemberInfo.Name;
     ClassType = MemberInfo.DeclaringType;
     TypeInfo  = TypesHelper.GetTypeInfo(OriginalType);
     if (TypeInfo.IsNullable)
     {
         Nullable   = true;
         MemberType = TypeInfo.NullableUnderlyingType.Type;
     }
     else
     {
         MemberType = OriginalType;
     }
     Getter = PreGetter;
     Setter = PreSetter;
 }
Exemplo n.º 4
0
 /// <summary> 初始化对象属性,字段访问组件,ignoreCase参数指示是否需要区分大小写
 /// </summary>
 /// <param name="type">需快速访问的类型</param>
 /// <param name="ignoreCase">是否区分大小写(不区分大小写时应保证类中没有同名的(仅大小写不同的)属性或字段)</param>
 public Literacy(Type type, bool ignoreCase)
     : this(TypesHelper.GetTypeInfo(type), ignoreCase)
 {
 }
Exemplo n.º 5
0
        private int                             _enumerIndex;  //如果参数是可遍历对象(IEnumerable) 用于表示索引

        public void AppendFormat(string number, string format, int offset = 0)
        {
            _enumerIndex  = 0;
            _formatString = number + ":" + format;
            //将number转为整型,且不能大于Arguments的个数
            int index;

            if (int.TryParse(number, out index) && index >= 0)
            {
                if (index >= Arguments.Length)
                {
                    throw new FormatException(GetErrorMessage("参数索引{" + number + "}过大"));
                }
            }
            else
            {
                throw new FormatException(GetErrorMessage("参数索引{" + number + "}错误"));
            }
            _number = offset == 0 ? number : (index + offset).ToString();

            //使用number得到参数
            var value = Arguments[index];

            ParseFormat(format);
            #region FormatNull
            //如果参数为null,则不能不是返回参数,也不能有name
            if (value == null || value is DBNull)
            {
                if (_name != null)
                {
                    throw new FormatException(GetErrorMessage("无法从<null>或<DBNull>中获取" + _name + "属性"));
                }
                if (_isOut)
                {
                    throw new FormatException(GetErrorMessage("无法从<null>或<DBNull>中推断返回值的类型"));
                }
                AppendValues((object)null);
                return;
            }
            #endregion

            //得到表示参数类型的TypeInfo对象
            var typeInfo = TypesHelper.GetTypeInfo(value.GetType());
            #region FormatSystemType
            if (typeInfo.IsSpecialType) //处理系统类型 ,不能有Name
            {
                if (_name != null)
                {
                    throw new FormatException(GetErrorMessage(typeInfo.DisplayName + "不支持参数名称"));
                }
                AppendValues(value);
                return;
            }
            #endregion

            //处理各种类型的参数
            switch (typeInfo.TypeCodes)
            {
            case TypeCodes.IList:
            case TypeCodes.IListT:
                if (_name != null)
                {
                    throw new FormatException(GetErrorMessage("当参数是 IEnumerable 类型时,不能使用参数名"));
                }
                AppendValues(value);
                return;

            case TypeCodes.IDictionary:
            case TypeCodes.IDictionaryT:
                var dict = value as IDictionary;
                if (dict == null)
                {
                    throw new FormatException(GetErrorMessage("当参数实现 IDictionary<,> 同时也必须实现 IDictionary"));
                }
                if (_name == null)
                {
                    throw new FormatException(GetErrorMessage("当参数是 IDictionary 类型时,必须设定键名称"));
                }
                if (dict.Contains(_name) == false)
                {
                    throw new FormatException(GetErrorMessage("没有找到元素 " + _name));
                }
                value = dict[_name];
                if (_isOut)
                {
                    if (dict.IsReadOnly)
                    {
                        throw new ReadOnlyException(GetErrorMessage("参数为只读,不能使用关键字out/ref"));
                    }
                    if (value == null)
                    {
                        throw new FormatException(GetErrorMessage("无法从<NULL>中推断返回值的类型"));
                    }
                    var p = value as DbParameter;
                    if (p != null)
                    {
                        AppendValues(p);
                        return;
                    }
                    p = AppendValues(value as Type ?? value.GetType());
                    if (p != null)
                    {
                        var name = _name;
                        Callback += delegate { dict[name] = p.Value; };
                    }
                }
                else
                {
                    AppendValues(value);
                }
                return;

            case TypeCodes.DbParameter:
                var dp = (DbParameter)value;
                if (dp.ParameterName != null && _name != null)
                {
                    throw new FormatException("DbParameter名称已存在,命名参数无效");
                }
                AppendValues(dp);
                return;

            case TypeCodes.AnonymousType:
                AppendObjectByName(value, typeInfo);
                return;

            default:
                if (value is IEnumerable)
                {
                    if (_name != null)
                    {
                        throw new FormatException(GetErrorMessage("当参数是 IEnumerable 类型时,不能使用参数名"));
                    }
                    AppendValues(value);
                }
                else
                {
                    AppendObjectByName(value, typeInfo);
                }
                return;
            }
        }
Exemplo n.º 6
0
        private JsonType(Type type, int i)
        {
            AreNull(type, "type");
            Type     = type;
            TypeInfo = TypesHelper.GetTypeInfo(type);
            _members = new Dictionary <string, JsonMember>(StringComparer.OrdinalIgnoreCase);
            var list = new List <JsonMember>();

            //枚举属性
            foreach (var p in Type.GetProperties())
            {
                var jm = JsonMember.Create(p);
                if (jm != null)
                {
                    AreTrue(_members.ContainsKey(jm.JsonName), "JsonName重复:" + jm.JsonName);
                    _members[jm.JsonName] = jm;
                    list.Add(jm);
                }
            }
            PropertyCount = list.Count;
            //枚举字段
            foreach (var p in Type.GetFields())
            {
                var jm = JsonMember.Create(p);
                if (jm != null)
                {
                    AreTrue(_members.ContainsKey(jm.JsonName), "JsonName重复:" + jm.JsonName);
                    _members[jm.JsonName] = jm;
                    list.Add(jm);
                }
            }
            Members = list.ToArray();
            //设置 TypeCodes ,ElementType ,KeyType
            switch (TypeCodes = TypeInfo.TypeCodes)
            {
            case TypeCodes.IListT:
                IsList = true;
                var args = SearchGenericInterface(type, typeof(IList <>)).GetGenericArguments();
                ElementType = JsonType.Get(args[0]);
                AddValue    = Literacy.CreateCaller(type.GetMethod("Add", args));
                break;

            case TypeCodes.IList:
                IsList = true;
                if (type.IsArray)
                {
                    ElementType = JsonType.Get(type.GetElementType());
                    AddValue    = (o, v) => ((System.Collections.ArrayList)o).Add(v[0]);
                }
                else
                {
                    ElementType = JsonType.Get(typeof(object));
                    AddValue    = (o, v) => ((System.Collections.IList)o).Add(v[0]);
                }
                break;

            case TypeCodes.IDictionary:
                IsDictionary = true;
                KeyType      = JsonType.Get(typeof(object));
                ElementType  = KeyType;
                AddKeyValue  = (o, v) => { ((System.Collections.IDictionary)o).Add(v[0], v[1]); return(null); };
                break;

            case TypeCodes.IDictionaryT:
                IsDictionary = true;
                var dictType = SearchGenericInterface(type, typeof(IDictionary <,>));
                args        = dictType.GetGenericArguments();
                KeyType     = JsonType.Get(args[0]);
                ElementType = JsonType.Get(args[1]);
                AddKeyValue = Literacy.CreateCaller(dictType.GetMethod("Add", args), type);
                break;

            default:
                break;
            }
        }