From ae23bb32643368208d63b12b555811edc510590f Mon Sep 17 00:00:00 2001 From: Trais McAllister Date: Mon, 20 Nov 2023 22:45:45 -0800 Subject: [PATCH] Added null handling (#27) --- AdapterSdk.Core/AdapterSdk.Core.csproj | 5 +++++ AdapterSdk/AdapterExtensions.cs | 10 ++++++++++ AdapterSdk/AdapterSdk.csproj | 2 +- AdapterSdk/DataItemLookup.cs | 10 +++++----- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/AdapterSdk.Core/AdapterSdk.Core.csproj b/AdapterSdk.Core/AdapterSdk.Core.csproj index 08d19e1..1e5b969 100644 --- a/AdapterSdk.Core/AdapterSdk.Core.csproj +++ b/AdapterSdk.Core/AdapterSdk.Core.csproj @@ -31,6 +31,7 @@ + @@ -47,6 +48,10 @@ + + + + diff --git a/AdapterSdk/AdapterExtensions.cs b/AdapterSdk/AdapterExtensions.cs index 11be143..ef05b05 100644 --- a/AdapterSdk/AdapterExtensions.cs +++ b/AdapterSdk/AdapterExtensions.cs @@ -435,6 +435,11 @@ private static bool TryUpdateValues(this IAdapter adapter, object model, string { // Dictionary var dictionary = property.GetValue(model) as IDictionary; + if (dictionary == null) + { + dataItemUpdated = false; + continue; + } foreach (DictionaryEntry entry in dictionary) { string dataItemSuffix = (string)entry.Key; @@ -445,6 +450,11 @@ private static bool TryUpdateValues(this IAdapter adapter, object model, string { // List var list = property.GetValue(model) as IList; + if (list == null) + { + dataItemUpdated = false; + continue; + } for (int i = 0; i < list.Count; i++) { adapter[dataItemName + i.ToString()].Value = list[i]; diff --git a/AdapterSdk/AdapterSdk.csproj b/AdapterSdk/AdapterSdk.csproj index 41e687d..1a6fbd5 100644 --- a/AdapterSdk/AdapterSdk.csproj +++ b/AdapterSdk/AdapterSdk.csproj @@ -21,7 +21,7 @@ icon.ico https://github.com/TrueAnalyticsSolutions/MtconnectCore.Adapter $(ProjectUrl) - 3.0.1.0 + 3.0.1.1 True snupkg true diff --git a/AdapterSdk/DataItemLookup.cs b/AdapterSdk/DataItemLookup.cs index ffab326..e25ea92 100644 --- a/AdapterSdk/DataItemLookup.cs +++ b/AdapterSdk/DataItemLookup.cs @@ -91,7 +91,7 @@ public void Add(IDataItem dataItem) deviceItems.Add(index); // Add to byNameAndDevicePrefix dictionary - _byNameAndDevicePrefix[(dataItem.Name, dataItem.DevicePrefix)] = index; + _byNameAndDevicePrefix[(dataItem.Name, dataItem.DevicePrefix ?? string.Empty)] = index; } /// @@ -145,7 +145,7 @@ public bool TryGetByNameAndDevicePrefix(string internalName, string devicePrefix { dataItem = null; - if (_byNameAndDevicePrefix.TryGetValue((internalName, devicePrefix), out int index)) + if (_byNameAndDevicePrefix.TryGetValue((internalName, devicePrefix ?? string.Empty), out int index)) { dataItem = _dataItems[index]; return true; @@ -207,7 +207,7 @@ public void RemoveAt(int index) if (dataItem != null) { // Update index in _byDeviceAndName dictionary - _byNameAndDevicePrefix[(dataItem.Name, dataItem.DevicePrefix)] = i; + _byNameAndDevicePrefix[(dataItem.Name, dataItem.DevicePrefix ?? string.Empty)] = i; // Update indices in _byName dictionary if (_byName.TryGetValue(dataItem.Name, out var list)) @@ -230,7 +230,7 @@ public void Clear() /// public bool Contains(IDataItem item) - => _byNameAndDevicePrefix.ContainsKey((item.Name, item.DevicePrefix)); + => _byNameAndDevicePrefix.ContainsKey((item.Name, item.DevicePrefix ?? string.Empty)); /// public bool ContainsName(string internalName) => _byName.ContainsKey(internalName); @@ -239,7 +239,7 @@ public bool Contains(IDataItem item) public bool ContainsDevicePrefix(string devicePrefix) => _byDevicePrefix.ContainsKey(devicePrefix); /// - public bool Contains(string internalName, string devicePrefix) => _byNameAndDevicePrefix.ContainsKey((internalName, devicePrefix)); + public bool Contains(string internalName, string devicePrefix) => _byNameAndDevicePrefix.ContainsKey((internalName, devicePrefix ?? string.Empty)); /// public void CopyTo(IDataItem[] array, int arrayIndex) => _dataItems.CopyTo(array, arrayIndex);