From 7821b6b6f6fe76a8cc521e2fedd621a9c6312b9d Mon Sep 17 00:00:00 2001 From: Michael Croes Date: Sat, 30 Jun 2018 21:40:22 +0200 Subject: [PATCH] Add NetStandard 1.3 support Supersedes UWP support since UWP 10.0 supports up to NetStandard 1.4. --- S7.Net/Compat/TcpClientMixins.cs | 19 +++++++++++++++++++ S7.Net/PLCExceptions.cs | 17 ++++++++++++----- S7.Net/S7.Net.csproj | 8 +++++--- S7.Net/Types/Class.cs | 4 ++-- S7.Net/Types/Struct.cs | 25 ++++++++++++++++++++++--- 5 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 S7.Net/Compat/TcpClientMixins.cs diff --git a/S7.Net/Compat/TcpClientMixins.cs b/S7.Net/Compat/TcpClientMixins.cs new file mode 100644 index 0000000..0b449e1 --- /dev/null +++ b/S7.Net/Compat/TcpClientMixins.cs @@ -0,0 +1,19 @@ +using System.Net.Sockets; + +namespace S7.Net +{ + public static class TcpClientMixins + { + #if NETSTANDARD1_3 + public static void Close(this TcpClient tcpClient) + { + tcpClient.Dispose(); + } + + public static void Connect(this TcpClient tcpClient, string host, int port) + { + tcpClient.ConnectAsync(host, port).GetAwaiter().GetResult(); + } + #endif + } +} diff --git a/S7.Net/PLCExceptions.cs b/S7.Net/PLCExceptions.cs index e3b97eb..c06823b 100644 --- a/S7.Net/PLCExceptions.cs +++ b/S7.Net/PLCExceptions.cs @@ -1,9 +1,8 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.Serialization; -using System.Text; -using System.Threading.Tasks; +#if NET_FULL +using System.Runtime.Serialization; +#endif + namespace S7.Net { @@ -21,9 +20,11 @@ namespace S7.Net { } + #if NET_FULL protected WrongNumberOfBytesException(SerializationInfo info, StreamingContext context) : base(info, context) { } + #endif } internal class InvalidAddressException : Exception @@ -40,9 +41,11 @@ namespace S7.Net { } + #if NET_FULL protected InvalidAddressException(SerializationInfo info, StreamingContext context) : base(info, context) { } + #endif } internal class InvalidVariableTypeException : Exception @@ -59,9 +62,11 @@ namespace S7.Net { } + #if NET_FULL protected InvalidVariableTypeException(SerializationInfo info, StreamingContext context) : base(info, context) { } + #endif } internal class TPKTInvalidException : Exception @@ -78,8 +83,10 @@ namespace S7.Net { } + #if NET_FULL protected TPKTInvalidException(SerializationInfo info, StreamingContext context) : base(info, context) { } + #endif } } diff --git a/S7.Net/S7.Net.csproj b/S7.Net/S7.Net.csproj index 1bcf18e..1fd6fe5 100644 --- a/S7.Net/S7.Net.csproj +++ b/S7.Net/S7.Net.csproj @@ -1,11 +1,13 @@ - - + - net452;netstandard2.0 + net452;netstandard2.0;netstandard1.3 true Properties\S7.Net.snk S7.Net.UnitTest + + NET_FULL + \ No newline at end of file diff --git a/S7.Net/Types/Class.cs b/S7.Net/Types/Class.cs index 26f4742..40b73ed 100644 --- a/S7.Net/Types/Class.cs +++ b/S7.Net/Types/Class.cs @@ -13,8 +13,8 @@ namespace S7.Net.Types private static IEnumerable GetAccessableProperties(Type classType) { return classType -#if NETFX_CORE - .GetProperties().Where(p => p.GetSetMethod() != null); +#if NETSTANDARD1_3 + .GetTypeInfo().DeclaredProperties.Where(p => p.SetMethod != null); #else .GetProperties( BindingFlags.SetProperty | diff --git a/S7.Net/Types/Struct.cs b/S7.Net/Types/Struct.cs index 9ee05c2..c58eba6 100644 --- a/S7.Net/Types/Struct.cs +++ b/S7.Net/Types/Struct.cs @@ -19,7 +19,13 @@ namespace S7.Net.Types { double numBytes = 0.0; - System.Reflection.FieldInfo[] infos = structType.GetFields(); + var infos = structType + #if NETSTANDARD1_3 + .GetTypeInfo().DeclaredFields; + #else + .GetFields(); + #endif + foreach (System.Reflection.FieldInfo info in infos) { switch (info.FieldType.Name) @@ -80,7 +86,14 @@ namespace S7.Net.Types double numBytes = 0.0; object structValue = Activator.CreateInstance(structType); - System.Reflection.FieldInfo[] infos = structValue.GetType().GetFields(); + + var infos = structValue.GetType() + #if NETSTANDARD1_3 + .GetTypeInfo().DeclaredFields; + #else + .GetFields(); + #endif + foreach (System.Reflection.FieldInfo info in infos) { switch (info.FieldType.Name) @@ -182,7 +195,13 @@ namespace S7.Net.Types int bitPos = 0; double numBytes = 0.0; - System.Reflection.FieldInfo[] infos = type.GetFields(); + var infos = type + #if NETSTANDARD1_3 + .GetTypeInfo().DeclaredFields; + #else + .GetFields(); + #endif + foreach (System.Reflection.FieldInfo info in infos) { bytes2 = null;