Beispiel #1
0
        public TcpComposition(Type typeData, Func <int, byte[]> byteArrayFactory, BitConverterHelper bitConverterHelper, Type typeId = null)
        {
            var serializerType = typeof(TcpSerializer <>).MakeGenericType(typeData);

            _serializerMethod = serializerType.GetMethod(nameof(TcpSerializer <TcpComposition> .Serialize));

            if (_serializerMethod == null)
            {
                throw new NullReferenceException(nameof(_serializerMethod));
            }

            _serializer = Activator.CreateInstance(serializerType, bitConverterHelper, byteArrayFactory);

            if (typeId == null)
            {
                return;
            }

            var deserializerType = typeof(TcpDeserializer <,>).MakeGenericType(typeId, typeData);

            _deserializerMethod = deserializerType.GetMethod(nameof(TcpDeserializer <int, int> .Deserialize));
            _deserializerField  = typeof(ValueTuple <,>).MakeGenericType(typeId, typeData).GetField("Item2");

            if (_deserializerMethod == null)
            {
                throw new NullReferenceException(nameof(_deserializerMethod));
            }

            _deserializer = Activator.CreateInstance(deserializerType, bitConverterHelper);
        }
Beispiel #2
0
 public ReflectionHelper(Type typeData, Func <int, byte[]> byteArrayFactory, BitConverterHelper bitConverterHelper, Type typeId = null)
 {
     EnsureTypeHasRequiredAttributes(typeData);
     Properties      = GetTypeProperties(typeData, byteArrayFactory, bitConverterHelper, typeId);
     MetaLength      = Properties.Sum(p => p.Attribute.Length);
     LengthProperty  = Properties.SingleOrDefault(p => p.Attribute.TcpDataType == TcpDataType.Length);
     BodyProperty    = Properties.SingleOrDefault(p => p.Attribute.TcpDataType == TcpDataType.Body);
     ComposeProperty = Properties.SingleOrDefault(p => p.Attribute.TcpDataType == TcpDataType.Compose);
 }
Beispiel #3
0
 public TcpDeserializer(BitConverterHelper bitConverterHelper)
 {
     _bitConverter = bitConverterHelper;
     _reflection   = new ReflectionHelper(typeof(TData), null, bitConverterHelper, typeof(TId));
 }
Beispiel #4
0
 public TcpSerializer(BitConverterHelper bitConverterHelper, Func <int, byte[]> byteArrayFactory)
 {
     _byteArrayFactory = byteArrayFactory;
     _bitConverter     = bitConverterHelper;
     _reflection       = new ReflectionHelper(typeof(TData), byteArrayFactory, bitConverterHelper);
 }
Beispiel #5
0
        private static List <TcpProperty> GetTypeProperties(Type typeData, Func <int, byte[]> byteArrayFactory, BitConverterHelper bitConverterHelper, Type typeId = null)
        {
            var tcpProperties = new List <TcpProperty>();

            foreach (var property in typeData.GetProperties())
            {
                var attribute = GetTcpDataAttribute(property);

                if (attribute == null)
                {
                    continue;
                }

                TcpProperty tcpProperty;
                if (attribute.TcpDataType == TcpDataType.Compose && !property.PropertyType.IsPrimitive)
                {
                    var tcpComposition = new TcpComposition(property.PropertyType, byteArrayFactory, bitConverterHelper, typeId);
                    tcpProperty = new TcpProperty(property, attribute, typeData, tcpComposition);
                }
                else
                {
                    tcpProperty = new TcpProperty(property, attribute, typeData);
                }

                tcpProperties.Add(tcpProperty);
            }

            return(tcpProperties);
        }