public void TestCreateInstance() { object item1 = reflectionCache.CreateInstance((byte)MyTestEnum.Item1, null); (item1 is TestItem1).Should().BeTrue(); object item2 = reflectionCache.CreateInstance((byte)MyTestEnum.Item2, null); (item2 is TestItem2).Should().BeTrue(); object item3 = reflectionCache.CreateInstance(0x02, null); item3.Should().BeNull(); }
internal static Transaction DeserializeFrom(BinaryReader reader) { // Looking for type in reflection cache Transaction transaction = ReflectionCache.CreateInstance <Transaction>(reader.ReadByte()); if (transaction == null) { throw new FormatException(); } transaction.DeserializeUnsignedWithoutType(reader); transaction.Scripts = reader.ReadSerializableArray <Witness>(); transaction.OnDeserialized(); return(transaction); }
public object Deserialize(IBinarySerializer deserializer, BinaryReader reader, Type type, BinarySerializerSettings settings = null) { // Read transaction Type var tx = Cache.CreateInstance <Transaction>(reader.ReadByte()); tx.Deserialize(deserializer, reader, settings); return(tx); }
/// <summary> /// Deserializes an <see cref="TransactionAttribute"/> object from a <see cref="BinaryReader"/>. /// </summary> /// <param name="reader">The <see cref="BinaryReader"/> for reading data.</param> /// <returns>The deserialized attribute.</returns> public static TransactionAttribute DeserializeFrom(BinaryReader reader) { TransactionAttributeType type = (TransactionAttributeType)reader.ReadByte(); if (ReflectionCache <TransactionAttributeType> .CreateInstance(type) is not TransactionAttribute attribute) { throw new FormatException(); } attribute.DeserializeWithoutType(reader); return(attribute); }
public void TestCreateInstance2() { TestItem defaultItem = new TestItem1(); object item2 = ReflectionCache <MyTestEnum> .CreateInstance(MyTestEnum.Item2, defaultItem); (item2 is TestItem2).Should().BeTrue(); object item1 = ReflectionCache <MyTestEnum> .CreateInstance((MyTestEnum)0x02, new TestItem1()); (item1 is TestItem1).Should().BeTrue(); }
/// <summary> /// Deserializes an <see cref="WitnessCondition"/> object from a <see cref="MemoryReader"/>. /// </summary> /// <param name="reader">The <see cref="MemoryReader"/> for reading data.</param> /// <param name="maxNestDepth">The maximum nesting depth allowed during deserialization.</param> /// <returns>The deserialized <see cref="WitnessCondition"/>.</returns> public static WitnessCondition DeserializeFrom(ref MemoryReader reader, int maxNestDepth) { WitnessConditionType type = (WitnessConditionType)reader.ReadByte(); if (ReflectionCache <WitnessConditionType> .CreateInstance(type) is not WitnessCondition condition) { throw new FormatException(); } condition.DeserializeWithoutType(ref reader, maxNestDepth); return(condition); }
/// <summary> /// Converts the <see cref="WitnessCondition"/> from a JSON object. /// </summary> /// <param name="json">The <see cref="WitnessCondition"/> represented by a JSON object.</param> /// <returns>The converted <see cref="WitnessCondition"/>.</returns> public static WitnessCondition FromJson(JObject json) { WitnessConditionType type = Enum.Parse <WitnessConditionType>(json["type"].GetString()); if (ReflectionCache <WitnessConditionType> .CreateInstance(type) is not WitnessCondition condition) { throw new FormatException("Invalid WitnessConditionType."); } condition.ParseJson(json); return(condition); }
public void TestCreateInstance() { object item1 = ReflectionCache <MyTestEnum> .CreateInstance(MyTestEnum.Item1, null); (item1 is TestItem1).Should().BeTrue(); object item2 = ReflectionCache <MyTestEnum> .CreateInstance(MyTestEnum.Item2, null); (item2 is TestItem2).Should().BeTrue(); object item3 = ReflectionCache <MyTestEnum> .CreateInstance((MyTestEnum)0x02, null); item3.Should().BeNull(); }
public static ConsensusMessage DeserializeFrom(byte[] data) { ConsensusMessage message = ReflectionCache.CreateInstance <ConsensusMessage>(data[0]); if (message == null) { throw new FormatException(); } using (MemoryStream ms = new MemoryStream(data, false)) using (BinaryReader r = new BinaryReader(ms)) { message.Deserialize(r); } return(message); }
public static unsafe MPTNode Decode(ReadOnlySpan <byte> data) { if (data.IsEmpty) { return(null); fixed(byte *pointer = data) { using UnmanagedMemoryStream stream = new UnmanagedMemoryStream(pointer, data.Length); using BinaryReader reader = new BinaryReader(stream); MPTNode n = (MPTNode)ReflectionCache <NodeType> .CreateInstance((NodeType)reader.ReadByte()); if (n is null) { throw new InvalidOperationException(); } n.DecodeSpecific(reader); return(n); } }