From b6b9049de82f6a48b3ccacd86cfdc75fa4b0ce6e Mon Sep 17 00:00:00 2001 From: David Hall Date: Fri, 7 Dec 2018 20:59:30 -0700 Subject: [PATCH] Fixed bug with COM objects and GetPropertyValue --- Core/Extensions/ReflectionExtensions.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Core/Extensions/ReflectionExtensions.cs b/Core/Extensions/ReflectionExtensions.cs index 878c6ea9..4ebcb9d2 100644 --- a/Core/Extensions/ReflectionExtensions.cs +++ b/Core/Extensions/ReflectionExtensions.cs @@ -40,11 +40,9 @@ namespace Vanara.Extensions /// The property value, if found, or the if not. public static T GetPropertyValue(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 (prop == null) return defaultValue; - var val = prop.GetValue(obj, null); - if (val == null) return defaultValue; - return (T)val; + if (obj is null || propertyName is 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)); } + catch { return defaultValue; } } ///