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); }
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); }
static int CalculateRealLength(TcpProperty lengthProperty, ref TData data, int metaLength, object dataLength) { var lengthValue = lengthProperty.PropertyType == typeof(int) ? dataLength : Convert.ChangeType(dataLength, lengthProperty.PropertyType); if (lengthProperty.IsValueType) { data = (TData)lengthProperty.Set(data, lengthValue); } else { lengthProperty.Set(data, lengthValue); } try { return((int)lengthValue + metaLength); } catch (InvalidCastException) { return(Convert.ToInt32(lengthValue) + metaLength); } }