/// <summary> 初始化对象属性,字段访问组件,ignoreCase参数指示是否需要区分大小写 /// </summary> /// <param name="type">需快速访问的类型</param> /// <param name="ignoreCase">是否区分大小写(不区分大小写时应保证类中没有同名的(仅大小写不同的)属性或字段)</param> public Literacy(Type type, bool ignoreCase) : this(TypesHelper.GetTypeInfo(type), ignoreCase) { }
/// <summary> 解释 json 字符串,并填充到obj对象中,如果obj为null则新建对象 /// </summary> private void FillObject(ref object obj, Type type, UnsafeJsonReader reader) { reader.CheckEnd(); var toObject = obj as ILoadJson; if (toObject != null) { obj = null; type = null; } if (type == null) { switch (reader.Current) { case '{': type = _keyValueType.Type; break; case '[': // 中括号的json仅支持反序列化成IList的对象 type = _arrayType.Type; break; default: ThrowException("Json字符串必须以 { 或 [ 开始"); break; } } else if (TypesHelper.IsChild(typeof(ILoadJson), type)) { obj = Activator.CreateInstance(type); FillObject(ref obj, null, reader); return; } var jsonType = JsonType.Get(type); //如果obj == null创建新对象 if (obj == null) { if (jsonType.TypeCodes == TypeCodes.AnonymousType) { throw new NotSupportedException("不支持匿名类型的反序列化操作"); } obj = jsonType.CreateInstance(); } //判断起始字符 switch (reader.Current) { case '{': reader.MoveNext(); if (jsonType.IsDictionary) { FillDictionary(obj, jsonType, reader); } else { FillProperty(obj, jsonType, reader); } reader.SkipChar('}', true); break; case '[': // 中括号的json仅支持反序列化成IList的对象 reader.MoveNext(); if (jsonType.Type.IsArray) { if (obj is ArrayList) { FillList(obj, jsonType, reader); } else { var arr = obj as Array; if (arr == null) { ThrowException("无法处理当前对象 : 类型( " + TypesHelper.DisplayName(obj.GetType()) + " )"); } FillArray(arr, jsonType, reader); } } else if (jsonType.IsList) { FillList(obj, jsonType, reader); } else { ThrowException("无法处理当前对象 : 类型( " + TypesHelper.DisplayName(obj.GetType()) + " )"); } reader.SkipChar(']', true); break; default: ThrowException("Json字符串必须以 { 或 [ 开始"); break; } if (toObject != null) { toObject.LoadJson(JsonObject.ToJsonObject(obj)); obj = toObject; } }
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; } }
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; } }