/// <summary> 从指定的 Type 创建新的 JsonType 对象,该方法必须保证类型公开的构造函数有且只有一个 /// </summary> public JsonType(Type type) : this(type, 0) { _ctor = Literacy.CreateNewObject(type); if (_ctor == null) { if (TypeInfo.IsSpecialType) { return; } var ctors = type.GetConstructors(); switch (ctors.Length) { case 0: _ctor = args => { throw new TypeInitializationException(TypesHelper.DisplayName(type), new MissingMethodException("当前类型没有构造函数")); }; break; case 1: _ctor = Literacy.CreateNewObject(ctors[0]); break; default: _ctor = args => { throw new TypeInitializationException(TypesHelper.DisplayName(type), new MethodAccessException("构造函数调用不明确")); }; break; } } }
private void Initialize() { Assertor.AreNull(_DBHelper, "DBHelper"); var factory = _DBHelper as IDbComponentFactory; if (factory == null) { if (_DBHelper is SqlServerHelper) { _FQLProvider = SqlServerFQL.Instance; _Saw = null; return; } throw new NotSupportedException(TypesHelper.DisplayName(_DBHelper.GetType()) + " 没有实现 IDbComponentFactory 接口"); } _FQLProvider = factory.CreateFQLProvider(); _Saw = factory.CreateSaw(); }
/// <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; } }