diff --git a/ICD.Common.Utils/Extensions/ReflectionExtensions.cs b/ICD.Common.Utils/Extensions/ReflectionExtensions.cs index df4a8ea..532c96e 100644 --- a/ICD.Common.Utils/Extensions/ReflectionExtensions.cs +++ b/ICD.Common.Utils/Extensions/ReflectionExtensions.cs @@ -175,6 +175,40 @@ namespace ICD.Common.Utils.Extensions return true; } + /// + /// Gets the PropertyInfo of the specified property + /// Traverses the path to access properties nested in other properties + /// + /// + /// + /// true if property get was successful, false if the property was not found + [CanBeNull] + public static PropertyInfo GetPropertyInfo([NotNull] this object extends, [NotNull] params string[] path) + { + if (extends == null) + throw new ArgumentNullException(nameof(extends)); + if (path == null) + throw new ArgumentNullException(nameof(path)); + + object currentObject = extends; + + //Grab property values until the last item in the path + for (int i = 0; i < path.Length - 1; i++) + { + PropertyInfo info = currentObject.GetType() +#if SIMPLSHARP + .GetCType() +#endif + .GetProperty(path[i]); + if (info == null) + return null; + currentObject = info.GetValue(currentObject); + } + + //Set the property to the value + return currentObject.GetType().GetProperty(path[path.Length - 1]); + } + /// /// Gets the value of a property /// Traverses the path to access properties nested in other properties