private static Type MapIdentifierToType(DSReader reader) { var type = (TypeIdentifier)reader.Read7bUInt(); switch(type) { case TypeIdentifier.Blob: return typeof(Blob); case TypeIdentifier.Compound: return typeof(Compound); case TypeIdentifier.Decimal: return typeof(Decimal); case TypeIdentifier.Dictionary: var kt = MapIdentifierToType(reader); var vt = MapIdentifierToType(reader); return typeof(Dictionary<,>).MakeGenericType(kt, vt); case TypeIdentifier.Double: return typeof(Double); case TypeIdentifier.Int: return typeof(SignedInteger); case TypeIdentifier.List: return typeof(List<>).MakeGenericType(MapIdentifierToType(reader)); case TypeIdentifier.String: return typeof(String); case TypeIdentifier.UInt: return typeof(UnsignedInteger); case TypeIdentifier.Undefined: return typeof(StoragePremitive); default: throw new ArgumentException(); } }
private static Func<DSReader, StoragePremitive> MapIdentifierToConstructor(DSReader reader) { var type = (TypeIdentifier)reader.Read7bUInt(); switch(type) { case TypeIdentifier.Blob: return x => new Blob(x); case TypeIdentifier.Compound: return x => new Compound(x); case TypeIdentifier.Decimal: return x => new Decimal(x); case TypeIdentifier.Double: return x => new Double(x); case TypeIdentifier.Int: return x => new SignedInteger(x); case TypeIdentifier.List: return x => { try { return (StoragePremitive)typeof(List<>).MakeGenericType(MapIdentifierToType(x)).GetConstructor(new Type[] { typeof(DSReader) }).Invoke(new object[] { x }); } catch (System.Reflection.TargetInvocationException e) { if (e.GetBaseException() is System.IO.IOException) { throw new System.IO.IOException("IO Error", e.GetBaseException()); } else throw; } }; case TypeIdentifier.String: return x => new String(x); case TypeIdentifier.UInt: return x => new UnsignedInteger(x); case TypeIdentifier.Null: return x => null; case TypeIdentifier.Dictionary: return x => { try { var kt = MapIdentifierToType(reader); var vt = MapIdentifierToType(reader); return (StoragePremitive)typeof(Dictionary<,>).MakeGenericType(kt, vt).GetConstructor(new Type[] { typeof(DSReader) }).Invoke(new object[] { x }); } catch(System.Reflection.TargetInvocationException e) { if (e.GetBaseException() is System.IO.IOException) { throw new System.IO.IOException("IO Error", e.GetBaseException()); } else throw; } }; default: throw new ArgumentException(); } }
public Blob(DSReader input) { data = new byte[(int)input.Read7bUInt()]; input.ReadFully(data); }