Changes following code review

This commit is contained in:
diego
2022-06-17 11:08:09 +02:00
parent d808ea5eb6
commit 2ecd2c6b49

View File

@@ -9,10 +9,17 @@ namespace S7.Net.Types
/// </summary>
public static class S7String
{
private static Encoding stringEncoding;
/// <summary>
/// The Encoding used when serializing and deserializing S7String (Encoding.ASCII by default)
/// </summary>
public static Encoding StringEncoding { get; set; }
/// <exception cref="ArgumentNullException">StringEncoding must not be null</exception>
public static Encoding StringEncoding
{
get => stringEncoding ??= Encoding.ASCII;
set => stringEncoding = value ?? throw new ArgumentNullException(nameof(StringEncoding));
}
/// <summary>
/// Converts S7 bytes to a string
@@ -35,7 +42,6 @@ namespace S7.Net.Types
try
{
if (StringEncoding == null) StringEncoding = Encoding.ASCII;
return StringEncoding.GetString(bytes, 2, length);
}
catch (Exception e)
@@ -61,7 +67,6 @@ namespace S7.Net.Types
if (reservedLength > 254) throw new ArgumentException($"The maximum string length supported is 254.");
if (StringEncoding == null) StringEncoding = Encoding.ASCII;
var bytes = StringEncoding.GetBytes(value);
if (bytes.Length > reservedLength) throw new ArgumentException($"The provided string length ({bytes.Length} is larger than the specified reserved length ({reservedLength}).");
@@ -71,13 +76,5 @@ namespace S7.Net.Types
buffer[1] = (byte)bytes.Length;
return buffer;
}
/// <summary>
/// Initializes default static properties
/// </summary>
static S7String()
{
StringEncoding = Encoding.ASCII;
}
}
}