diff --git a/Modbus.Net/Modbus.Net/ValueHelper.cs b/Modbus.Net/Modbus.Net/ValueHelper.cs
index 81b52b5..a59a4cc 100644
--- a/Modbus.Net/Modbus.Net/ValueHelper.cs
+++ b/Modbus.Net/Modbus.Net/ValueHelper.cs
@@ -37,12 +37,14 @@ namespace Modbus.Net
#region Factory
- protected static ValueHelper _Instance;
+ private static ValueHelper _instance;
+
+ protected virtual ValueHelper _Instance => _instance;
///
/// ValueHelper单例的实例
///
- public static ValueHelper Instance => _Instance ?? (_Instance = new ValueHelper());
+ public static ValueHelper Instance => _instance ?? (_instance = new ValueHelper());
#endregion
@@ -144,61 +146,51 @@ namespace Modbus.Net
///
public virtual Byte[] GetBytes(object value, Type type)
{
- ValueHelper Instance;
- if (this is BigEndianValueHelper)
- {
- Instance = BigEndianValueHelper.Instance;
- }
- else
- {
- Instance = ValueHelper.Instance;
- }
-
switch (type.FullName)
{
case "System.Int16":
{
- byte[] bytes = Instance.GetBytes((short) value);
+ byte[] bytes = _Instance.GetBytes((short) value);
return bytes;
}
case "System.Int32":
{
- byte[] bytes = Instance.GetBytes((int) value);
+ byte[] bytes = _Instance.GetBytes((int) value);
return bytes;
}
case "System.Int64":
{
- byte[] bytes = Instance.GetBytes((long) value);
+ byte[] bytes = _Instance.GetBytes((long) value);
return bytes;
}
case "System.UInt16":
{
- byte[] bytes = Instance.GetBytes((ushort) value);
+ byte[] bytes = _Instance.GetBytes((ushort) value);
return bytes;
}
case "System.UInt32":
{
- byte[] bytes = Instance.GetBytes((uint) value);
+ byte[] bytes = _Instance.GetBytes((uint) value);
return bytes;
}
case "System.UInt64":
{
- byte[] bytes = Instance.GetBytes((ulong) value);
+ byte[] bytes = _Instance.GetBytes((ulong) value);
return bytes;
}
case "System.Single":
{
- byte[] bytes = Instance.GetBytes((float) value);
+ byte[] bytes = _Instance.GetBytes((float) value);
return bytes;
}
case "System.Double":
{
- byte[] bytes = Instance.GetBytes((double) value);
+ byte[] bytes = _Instance.GetBytes((double) value);
return bytes;
}
case "System.Byte":
{
- byte[] bytes = Instance.GetBytes((byte) value);
+ byte[] bytes = _Instance.GetBytes((byte) value);
return bytes;
}
default:
@@ -217,61 +209,51 @@ namespace Modbus.Net
///
public virtual object GetValue(byte[] data, ref int pos, Type t)
{
- ValueHelper Instance;
- if (this is BigEndianValueHelper)
- {
- Instance = BigEndianValueHelper.Instance;
- }
- else
- {
- Instance = ValueHelper.Instance;
- }
-
switch (t.FullName)
{
case "System.Int16":
{
- short value = Instance.GetShort(data, ref pos);
+ short value = _Instance.GetShort(data, ref pos);
return value;
}
case "System.Int32":
{
- int value = Instance.GetInt(data, ref pos);
+ int value = _Instance.GetInt(data, ref pos);
return value;
}
case "System.Int64":
{
- long value = Instance.GetLong(data, ref pos);
+ long value = _Instance.GetLong(data, ref pos);
return value;
}
case "System.UInt16":
{
- ushort value = Instance.GetUShort(data, ref pos);
+ ushort value = _Instance.GetUShort(data, ref pos);
return value;
}
case "System.UInt32":
{
- uint value = Instance.GetUInt(data, ref pos);
+ uint value = _Instance.GetUInt(data, ref pos);
return value;
}
case "System.UInt64":
{
- ulong value = Instance.GetULong(data, ref pos);
+ ulong value = _Instance.GetULong(data, ref pos);
return value;
}
case "System.Single":
{
- float value = Instance.GetFloat(data, ref pos);
+ float value = _Instance.GetFloat(data, ref pos);
return value;
}
case "System.Double":
{
- double value = Instance.GetDouble(data, ref pos);
+ double value = _Instance.GetDouble(data, ref pos);
return value;
}
case "System.Byte":
{
- byte value = Instance.GetByte(data, ref pos);
+ byte value = _Instance.GetByte(data, ref pos);
return value;
}
default:
@@ -424,16 +406,6 @@ namespace Modbus.Net
///
public virtual byte[] ObjectArrayToByteArray(object[] contents)
{
- ValueHelper Instance;
- if (this is BigEndianValueHelper)
- {
- Instance = BigEndianValueHelper.Instance;
- }
- else
- {
- Instance = ValueHelper.Instance;
- }
-
bool b = false;
//先查找传入的结构中有没有数组,有的话将其打开
var newContentsList = new List