From 60ab7b586d910fdea0742b5f804d1185e9139173 Mon Sep 17 00:00:00 2001 From: Marcos Date: Sun, 13 Mar 2016 11:53:10 +0000 Subject: [PATCH] Rounding issue on GetClassSize fixed. Read classes recursively. --- S7.Net.Common/Types/Class.cs | 91 +++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/S7.Net.Common/Types/Class.cs b/S7.Net.Common/Types/Class.cs index e64a7cf..bd47432 100644 --- a/S7.Net.Common/Types/Class.cs +++ b/S7.Net.Common/Types/Class.cs @@ -12,57 +12,49 @@ namespace S7.Net.Types /// the number of bytes public static int GetClassSize(Type classType) { - double numBytes = 0.0; + double numBytes = 0.0; + var properties = classType.GetProperties(); foreach (var property in properties) { - numBytes += CalculateBytes(property.PropertyType.Name); + switch (property.PropertyType.Name) + { + case "Boolean": + numBytes += 0.125; + break; + case "Byte": + numBytes = Math.Ceiling(numBytes); + numBytes++; + break; + case "Int16": + case "UInt16": + numBytes = Math.Ceiling(numBytes); + if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0) + numBytes++; + numBytes += 2; + break; + case "Int32": + case "UInt32": + numBytes = Math.Ceiling(numBytes); + if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0) + numBytes++; + numBytes += 4; + break; + case "Float": + case "Double": + numBytes = Math.Ceiling(numBytes); + if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0) + numBytes++; + numBytes += 4; + break; + default: + numBytes += GetClassSize(property.PropertyType); + break; + } } return (int)numBytes; } - /// - /// Given a property name, it returns the number of bytes that is composed - /// - /// type of the property - /// - static double CalculateBytes(string propertyType) - { - double numBytes = 0; - switch (propertyType) - { - case "Boolean": - numBytes += 0.125; - break; - case "Byte": - numBytes = Math.Ceiling(numBytes); - numBytes++; - break; - case "Int16": - case "UInt16": - numBytes = Math.Ceiling(numBytes); - if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0) - numBytes++; - numBytes += 2; - break; - case "Int32": - case "UInt32": - numBytes = Math.Ceiling(numBytes); - if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0) - numBytes++; - numBytes += 4; - break; - case "Float": - case "Double": - numBytes = Math.Ceiling(numBytes); - if ((numBytes / 2 - Math.Floor(numBytes / 2.0)) > 0) - numBytes++; - numBytes += 4; - break; - } - return numBytes; - } - /// /// Creates a struct of a specified type by an array of bytes. /// @@ -155,6 +147,16 @@ namespace S7.Net.Types bytes[(int)numBytes + 3] }), null); numBytes += 4; break; + default: + var buffer = new byte[GetClassSize(property.PropertyType)]; + if (buffer.Length == 0) + continue; + Buffer.BlockCopy(bytes, (int)Math.Ceiling(numBytes), buffer, 0, buffer.Length); + var propClass = Activator.CreateInstance(property.PropertyType); + FromBytes(propClass, property.PropertyType, buffer); + property.SetValue(sourceClass, propClass, null); + numBytes += buffer.Length; + break; } } } @@ -179,6 +181,7 @@ namespace S7.Net.Types var properties = sourceClass.GetType().GetProperties(); foreach (var property in properties) { + bytes2 = null; switch (property.PropertyType.Name) { case "Boolean":