Skip to content

Commit

Permalink
refactor: remove unused object extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfbecker committed Sep 21, 2019
1 parent 631a51b commit 3cac530
Showing 1 changed file with 0 additions and 51 deletions.
51 changes: 0 additions & 51 deletions src/ObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;

namespace Kubectl {
Expand All @@ -27,54 +25,5 @@ public static object GetDynamicPropertyValue(this object obj, string propertyNam
}
return prop.GetValue(obj);
}

/// <summary>
/// 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.
/// </summary>
public static IEnumerable<string> 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);
}

/// <summary>
/// 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.
/// </summary>
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);
}
}
}
}

0 comments on commit 3cac530

Please sign in to comment.