protected static bool GetField()

in src/PSRule/Commands/RuleKeyword.cs [30:81]


        protected static bool GetField(object targetObject, string name, bool caseSensitive, out object value)
        {
            value = null;
            if (targetObject == null)
            {
                value = null;
                return false;
            }

            var comparer = caseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase;
            var baseObject = GetBaseObject(targetObject);
            var baseType = baseObject.GetType();

            // Handle dictionaries and hashtables
            if (typeof(IDictionary).IsAssignableFrom(baseType))
            {
                var dictionary = (IDictionary)baseObject;
                foreach (var key in dictionary.Keys)
                {
                    if (comparer.Equals(name, key))
                    {
                        value = dictionary[key];
                        return true;
                    }
                }
            }
            // Handle PSObjects
            else if (targetObject is PSObject pso)
            {
                foreach (var prop in pso.Properties)
                {
                    if (comparer.Equals(name, prop.Name))
                    {
                        value = prop.Value;
                        return true;
                    }
                }
            }
            // Handle all other CLR types
            else
            {
                foreach (var p in baseType.GetProperties())
                {
                    if (comparer.Equals(name, p.Name))
                    {
                        value = p.GetValue(targetObject);
                        return true;
                    }
                }
            }
            return false;
        }