Files
speckle-sharp-connectors/DUI3/Speckle.Connectors.DUI/Utils/PropertyValidator.cs
T
Adam Hathcock 891a18819b React to sdk renaming (#113)
* react to renamespacing

* merge dev

* fmt

* initialize things with typeloader

* autocad initialization

* add arcgis

* add more projects to local

* instrument rhino more

* update nugets

* fmt
2024-08-08 10:32:01 +00:00

52 lines
1.4 KiB
C#

using Speckle.Newtonsoft.Json;
using Speckle.Sdk.Common;
namespace Speckle.Connectors.DUI.Utils;
public class PropertyValidator : DiscriminatedObject
{
[JsonIgnore]
public List<string>? JsonPropertyNames { get; set; }
public bool InitializeNewProperties()
{
bool isUpdated = false;
var properties = this.GetType().GetProperties();
// Create a new instance of the current type to get default values
var defaultInstance = Activator.CreateInstance(this.GetType());
foreach (var property in properties)
{
if (property.GetValue(this) == null)
{
// Get the default value from the new instance
var defaultValue = property.GetValue(defaultInstance);
// Set this default value to the current instance
property.SetValue(this, defaultValue);
isUpdated = true;
}
}
return isUpdated; // Return true if any property was updated
}
public bool CheckRemovedProperties()
{
bool removedPropertiesExist = false;
var currentPropertyNames = this.GetType().GetProperties().Select(p => p.Name).ToList();
foreach (var jsonPropName in JsonPropertyNames.NotNull())
{
if (!currentPropertyNames.Contains(jsonPropName))
{
// This property was in the JSON but not in the class
removedPropertiesExist = true;
}
}
return removedPropertiesExist;
}
}