From 3cac530bd00f0fa24c2d94888bca7a3dde250cda Mon Sep 17 00:00:00 2001 From: Felix Becker Date: Sat, 21 Sep 2019 12:52:23 +0200 Subject: [PATCH] refactor: remove unused object extensions --- src/ObjectExtensions.cs | 51 ----------------------------------------- 1 file changed, 51 deletions(-) diff --git a/src/ObjectExtensions.cs b/src/ObjectExtensions.cs index 8e9d928..6856764 100644 --- a/src/ObjectExtensions.cs +++ b/src/ObjectExtensions.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using System.Management.Automation; namespace Kubectl { @@ -27,54 +25,5 @@ public static object GetDynamicPropertyValue(this object obj, string propertyNam } return prop.GetValue(obj); } - - /// - /// Retrieves the names of all properties from a .NET object or a PSObject. - /// For PSObject, will only return real and NoteProperties (no ScriptProperties). - /// Includes inherited members. - /// - public static IEnumerable GetDynamicPropertyNames(this object obj) { - if (obj == null) { - throw new ArgumentNullException(nameof(obj)); - } - - if (obj is PSObject psObject) { - return psObject.Properties - .Where(prop => prop.MemberType == PSMemberTypes.NoteProperty || prop.MemberType == PSMemberTypes.Property) - .Select(prop => prop.Name); - } - - return obj.GetType().GetProperties().Select(prop => prop.Name); - } - - /// - /// Sets a property value on a .NET object or a PSObject - /// If the property does not exist on the .NET object, will throw an Exception. - /// If the property does not exist on a PSObject, will return null. - /// - public static void SetDynamicPropertyValue(this object obj, string propertyName, object value) { - if (obj == null) { - throw new ArgumentNullException(nameof(obj)); - } - if (propertyName == null) { - throw new ArgumentNullException(nameof(obj)); - } - - if (obj is PSObject psObject) { - // Handle PSObject - if (psObject.Properties[propertyName] != null) { - psObject.Properties[propertyName].Value = value; - } else { - psObject.Properties.Add(new PSNoteProperty(propertyName, value)); - } - } else { - // Handle plain .NET object - var prop = obj.GetType().GetProperty(propertyName); - if (prop == null) { - throw new Exception($"Cannot set property value of unknown property {propertyName} on object of type {obj.GetType().Name}"); - } - prop.SetValue(obj, value); - } - } } }