mirror of https://github.com/dahall/Vanara.git
Separated GetPropertyValue into 2 separate methods and removed type changing to prevent unintended consequences.
parent
6f62808fc5
commit
39264cc1ab
|
@ -61,16 +61,26 @@ namespace Vanara.Extensions
|
||||||
bool ImplIEnumT(Type t) => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>);
|
bool ImplIEnumT(Type t) => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IEnumerable<>);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>Gets a named property value from an object.</summary>
|
||||||
|
/// <typeparam name="T">The expected type of the property to be returned.</typeparam>
|
||||||
|
/// <param name="obj">The object from which to retrieve the property.</param>
|
||||||
|
/// <param name="propertyName">Name of the property.</param>
|
||||||
|
/// <returns>The property value.</returns>
|
||||||
|
public static T GetPropertyValue<T>(this object obj, string propertyName)
|
||||||
|
{
|
||||||
|
if (obj is null) throw new ArgumentNullException(nameof(obj));
|
||||||
|
return (T)obj.GetType().InvokeMember(propertyName, BindingFlags.GetProperty | bindingFlags, null, obj, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Gets a named property value from an object.</summary>
|
/// <summary>Gets a named property value from an object.</summary>
|
||||||
/// <typeparam name="T">The expected type of the property to be returned.</typeparam>
|
/// <typeparam name="T">The expected type of the property to be returned.</typeparam>
|
||||||
/// <param name="obj">The object from which to retrieve the property.</param>
|
/// <param name="obj">The object from which to retrieve the property.</param>
|
||||||
/// <param name="propertyName">Name of the property.</param>
|
/// <param name="propertyName">Name of the property.</param>
|
||||||
/// <param name="defaultValue">The default value to return in the instance that the property is not found.</param>
|
/// <param name="defaultValue">The default value to return in the instance that the property is not found.</param>
|
||||||
/// <returns>The property value, if found, or the <paramref name="defaultValue"/> if not.</returns>
|
/// <returns>The property value, if found, or the <paramref name="defaultValue"/> if not.</returns>
|
||||||
public static T GetPropertyValue<T>(this object obj, string propertyName, T defaultValue = default)
|
public static T GetPropertyValue<T>(this object obj, string propertyName, T defaultValue)
|
||||||
{
|
{
|
||||||
if (obj is null || propertyName is null) return defaultValue;
|
try { return GetPropertyValue<T>(obj, propertyName); }
|
||||||
try { return (T)Convert.ChangeType(obj.GetType().InvokeMember(propertyName, BindingFlags.GetProperty | bindingFlags, null, obj, null, null), typeof(T)); }
|
|
||||||
catch { return defaultValue; }
|
catch { return defaultValue; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue