Beispiel #1
0
 public object GetTargetObject()
 {
     try
     {
         var stripped = ReflectionMethods.StripIndexer(TargetName);
         return(ParentType.GetRuntimeProperty(stripped).GetValue(ParentObject));
     }
     catch (Exception)
     {
         return(null);
     }
 }
        private static GetObjectResult GetObject(object parent, string propertyName, string inputName, bool includeStatics)
        {
            var result = new GetObjectResult
            {
                PropertyName = propertyName,
                Input        = parent,
                InputName    = inputName
            };

            object propVal = null;

            var isEnumerable = ReflectionMethods.EnumerablePattern.IsMatch(propertyName);
            var stripped     = ReflectionMethods.StripIndexer(propertyName);

            FieldInfo fieldInfo = null;

            var propInfo = parent
                           .GetType()
                           .GetPublicProperties(includeStatics)
                           .FirstOrDefault(p => ReflectionMethods.NameMatch(p, stripped));

            if (propInfo == null)
            {
                fieldInfo = parent
                            .GetType()
                            .GetFields()
                            .FirstOrDefault(p => ReflectionMethods.FieldNameMatch(p, stripped));
            }

            if (propInfo != null)
            {
                propVal = propInfo.GetValue(parent);
            }

            if (fieldInfo != null)
            {
                propVal = fieldInfo.GetValue(parent);
            }

            if (isEnumerable)
            {
                result.Property = propVal;

                var index = ReflectionMethods.GetIndexerValue(propertyName);
                if (index == null)
                {
                    return(null);
                }

                var item = ReflectionMethods.GetItem(propVal, index.Value);
                if (item == null)
                {
                    return(null);
                }

                result.Leaf = item;
            }
            else
            {
                result.Property = parent;
                result.Leaf     = propVal;
            }

            result.PropertyInfo = result.Input.GetType().GetProperty(stripped);

            return(result);
        }