mirror of https://github.com/dahall/Vanara.git
Fixed bug with COM objects and GetPropertyValue
parent
33b358033d
commit
b6b9049de8
|
@ -40,11 +40,9 @@ namespace Vanara.Extensions
|
||||||
/// <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 = default)
|
||||||
{
|
{
|
||||||
var prop = obj.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, typeof(T), Type.EmptyTypes, null);
|
if (obj is null || propertyName is null) return defaultValue;
|
||||||
if (prop == null) return defaultValue;
|
try { return (T)Convert.ChangeType(obj.GetType().InvokeMember(propertyName, BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, obj, null, null), typeof(T)); }
|
||||||
var val = prop.GetValue(obj, null);
|
catch { return defaultValue; }
|
||||||
if (val == null) return defaultValue;
|
|
||||||
return (T)val;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
Loading…
Reference in New Issue