コード例 #1
0
 public object GetTargetObject()
 {
     try
     {
         var stripped = ReflectionMethods.StripIndexer(TargetName);
         return(ParentType.GetRuntimeProperty(stripped).GetValue(ParentObject));
     }
     catch (Exception)
     {
         return(null);
     }
 }
コード例 #2
0
        public static object Construct(ITypeFinder finder, UIConstructor ctor)
        {
            var cType = finder.Find(ctor.TypeName);

            if (cType == null)
            {
                return(null);
            }
            object[] ps = null;

            if (ctor.Parameters.Any())
            {
                ps = ctor
                     .Parameters
                     .OrderBy(c => c.Position)
                     .Select(p =>
                {
                    if (p == null)
                    {
                        return(null);
                    }

                    var pType = finder.Find(p.TypeName);
                    if (pType == null)
                    {
                        return(null);
                    }
                    object val;

                    try
                    {
                        if (pType.GetTypeInfo().IsEnum)
                        {
                            val = ReflectionMethods.CreateEnum(pType, p.Value);
                        }
                        else
                        {
                            val = Convert.ChangeType(p.Value, pType);
                        }
                    }
                    catch (Exception)
                    {
                        val = p.Value;
                    }

                    return(val);
                }).ToArray();
            }

            return(Activator.CreateInstance(cType, ps));
        }
コード例 #3
0
        /// <summary>
        /// Return primitive value types, such as byte, short, int, long.
        /// String and DateTime are not considered primitive value types.
        /// </summary>
        /// <param name="value">struct</param>
        public static UIProperty[] GetPrimitiveValueTypeProperties(object value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            var type = value.GetType();

            UIProperty[] result = {};

            if (ReflectionMethods.IsNotPrimitiveValueType(value))
            {
                result = type
                         .GetPublicProperties()
                         .Where(p =>
                {
                    var pi = p.PropertyType.GetTypeInfo();
                    if (pi == null)
                    {
                        return(false);
                    }
                    return((pi.IsPrimitive && pi.IsValueType) || pi.IsEnum);
                })
                         .Select(p => new UIProperty
                {
                    UIType = new UIType
                    {
                        FullName = p.PropertyType.FullName,
                    },
                    Path     = new[] { p.Name },
                    CanRead  = p.CanRead,
                    CanWrite = p.CanWrite,
                    Value    = p.GetValue(value),
                })
                         .ToArray();
            }

            return(result);
        }
コード例 #4
0
        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);
        }