From 6e103cea63588102a5a7cfdfde732ab4bcbd3aa0 Mon Sep 17 00:00:00 2001 From: Michael Croes Date: Fri, 28 Jul 2023 23:55:12 +0200 Subject: [PATCH] fix: Fix warnings in Struct --- S7.Net/Types/Struct.cs | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/S7.Net/Types/Struct.cs b/S7.Net/Types/Struct.cs index 1e95508..6f29447 100644 --- a/S7.Net/Types/Struct.cs +++ b/S7.Net/Types/Struct.cs @@ -98,8 +98,8 @@ namespace S7.Net.Types int bytePos = 0; int bitPos = 0; double numBytes = 0.0; - object structValue = Activator.CreateInstance(structType); - + object structValue = Activator.CreateInstance(structType) ?? + throw new ArgumentException($"Failed to create an instance of the type {structType}.", nameof(structType)); var infos = structValue.GetType() #if NETSTANDARD1_3 @@ -254,6 +254,14 @@ namespace S7.Net.Types foreach (var info in infos) { + static TValue GetValueOrThrow(FieldInfo fi, object structValue) where TValue : struct + { + var value = fi.GetValue(structValue) as TValue? ?? + throw new ArgumentException($"Failed to convert value of field {fi.Name} of {structValue} to type {typeof(TValue)}"); + + return value; + } + bytes2 = null; switch (info.FieldType.Name) { @@ -261,7 +269,7 @@ namespace S7.Net.Types // get the value bytePos = (int)Math.Floor(numBytes); bitPos = (int)((numBytes - (double)bytePos) / 0.125); - if ((bool)info.GetValue(structValue)) + if (GetValueOrThrow(info, structValue)) bytes[bytePos] |= (byte)Math.Pow(2, bitPos); // is true else bytes[bytePos] &= (byte)(~(byte)Math.Pow(2, bitPos)); // is false @@ -270,26 +278,26 @@ namespace S7.Net.Types case "Byte": numBytes = (int)Math.Ceiling(numBytes); bytePos = (int)numBytes; - bytes[bytePos] = (byte)info.GetValue(structValue); + bytes[bytePos] = GetValueOrThrow(info, structValue); numBytes++; break; case "Int16": - bytes2 = Int.ToByteArray((Int16)info.GetValue(structValue)); + bytes2 = Int.ToByteArray(GetValueOrThrow(info, structValue)); break; case "UInt16": - bytes2 = Word.ToByteArray((UInt16)info.GetValue(structValue)); + bytes2 = Word.ToByteArray(GetValueOrThrow(info, structValue)); break; case "Int32": - bytes2 = DInt.ToByteArray((Int32)info.GetValue(structValue)); + bytes2 = DInt.ToByteArray(GetValueOrThrow(info, structValue)); break; case "UInt32": - bytes2 = DWord.ToByteArray((UInt32)info.GetValue(structValue)); + bytes2 = DWord.ToByteArray(GetValueOrThrow(info, structValue)); break; case "Single": - bytes2 = Real.ToByteArray((float)info.GetValue(structValue)); + bytes2 = Real.ToByteArray(GetValueOrThrow(info, structValue)); break; case "Double": - bytes2 = LReal.ToByteArray((double)info.GetValue(structValue)); + bytes2 = LReal.ToByteArray(GetValueOrThrow(info, structValue)); break; case "String": S7StringAttribute? attribute = info.GetCustomAttributes().SingleOrDefault(); @@ -298,8 +306,8 @@ namespace S7.Net.Types bytes2 = attribute.Type switch { - S7StringType.S7String => S7String.ToByteArray((string)info.GetValue(structValue), attribute.ReservedLength), - S7StringType.S7WString => S7WString.ToByteArray((string)info.GetValue(structValue), attribute.ReservedLength), + S7StringType.S7String => S7String.ToByteArray((string?)info.GetValue(structValue), attribute.ReservedLength), + S7StringType.S7WString => S7WString.ToByteArray((string?)info.GetValue(structValue), attribute.ReservedLength), _ => throw new ArgumentException("Please use a valid string type for the S7StringAttribute") }; break;