Beispiel #1
0
        public void Test_GetPrimitiveValueTypeProperties()
        {
            var v1      = new TestValueType();
            var v1Props = XenPropertyMethods.GetPrimitiveValueTypeProperties(v1);

            Assert.AreEqual(2, v1Props.Length);

            var int1 = v1Props.FirstOrDefault(p => p.PropertyName == "Int1");
            var int2 = v1Props.FirstOrDefault(p => p.PropertyName == "Int2");

            Assert.IsNotNull(int1, "Int1");
            Assert.IsNotNull(int2, "Int2");
        }
Beispiel #2
0
        public XenProperty[] GetXenProperties(XenReflectionProperty[] refProps, bool includeValues = false)
        {
            if (refProps == null)
            {
                return(null);
            }
            if (SupportingTypes.Types == null)
            {
                return(null);
            }

            var result = new List <XenProperty>();

            // The properties are in an intermediate state, right here.
            // a XenReflectionProperty is not meant to be returned in a request.

            // build xen properties.
            foreach (var curRef in refProps)
            {
                if (curRef.IsTargetEnum && SupportingTypes.IsRegistered(typeof(Enum)))
                {
                    if (!SupportingTypes.IsRegistered(curRef, RegistrarMatches.TypeName | RegistrarMatches.Enum))
                    {
                        continue;
                    }
                }
                else
                {
                    if (!SupportingTypes.IsRegistered(curRef, RegistrarMatches.TypeName))
                    {
                        continue;
                    }
                }

                var xenProp = new XenProperty
                {
                    Path     = new[] { curRef.TargetName },
                    Value    = curRef.GetTargetObject(),
                    CanRead  = curRef.CanReadTarget,
                    CanWrite = curRef.CanWriteTarget
                };

                // is the current property an enumeration?
                if (curRef.IsTargetEnum && SupportingTypes.IsRegistered(typeof(Enum)))
                {
                    var value = curRef.As <Enum>();
                    if (value != null)
                    {
                        Descriptor.SetPossibleValues(curRef, xenProp, value);

                        if (xenProp.Value != null)
                        {
                            xenProp.Value = Enum.GetName(xenProp.Value.GetType(), value);
                        }
                    }
                }
                else
                {
                    Descriptor.SetPossibleValues(curRef, xenProp);

                    // non-primitive structures (not System.DateTime, but Xamarin.Forms.Point)
                    if (ReflectionMethods.IsNotPrimitiveValueType(xenProp.Value))
                    {
                        xenProp.XenType.Descriptor |= XenPropertyDescriptors.ValueType;

                        var vtProps = XenPropertyMethods
                                      .GetPrimitiveValueTypeProperties(xenProp.Value)
                                      .ToArray();

                        if (vtProps.Length != 0)
                        {
                            foreach (var vtProp in vtProps)
                            {
                                Descriptor.SetPossibleValues(vtProp);

                                if (vtProp.Value.GetType() != typeof(string))
                                {
                                    var tmp = Convert.ToString(vtProp.Value ?? string.Empty);
                                    vtProp.Value = tmp;
                                }
                            }

                            // don't return values to the toolbox that will never be used.
                            xenProp.Value = includeValues ? vtProps : null;
                        }
                    }

                    // enumerables, collections, list.
                    if (curRef.TargetType.IsKindOf(typeof(ICollection <>)))
                    {
                        xenProp.XenType.Descriptor |= XenPropertyDescriptors.Collection;
                    }

                    if (curRef.TargetType.IsKindOf(typeof(IList <>)))
                    {
                        xenProp.XenType.Descriptor |= XenPropertyDescriptors.List;
                    }

                    if (curRef.TargetType.IsKindOf(typeof(IEnumerable <>)))
                    {
                        xenProp.XenType.Descriptor |= XenPropertyDescriptors.Enumerable;
                        var collection = xenProp.Value as IEnumerable <object>;

                        if (collection != null)
                        {
                            var count = collection.Count();
                            xenProp.XenType.PossibleValues = new[] { count.ToString() };

                            if (includeValues == false)
                            {
                                // don't send the value back to the toolbox
                                xenProp.Value = null;
                            }
                        }
                    }

                    /*
                     * obj support?
                     * if (!curRef.TargetType.GetTypeInfo().IsValueType && !curRef.TargetType.GetTypeInfo().IsPrimitive)
                     * {
                     *  if (!curRef.TargetType.Namespace.StartsWith("System"))
                     *  {
                     *      var oProps = XenPropertyMethods
                     *      .GetObjectProperties(xenProp.Value)
                     *      .ToArray();
                     *
                     *      if (oProps.Length != 0)
                     *      {
                     *          foreach (var oProp in oProps)
                     *          {
                     *              Descriptor.SetPossibleValues(oProp);
                     *
                     *              if (oProp.Value != null && oProp.Value.GetType() != typeof(string))
                     *              {
                     *                  var tmp = Convert.ToString(oProp.Value ?? string.Empty);
                     *                  oProp.Value = tmp;
                     *              }
                     *          }
                     *
                     *          // don't return values to the toolbox that will never be used.
                     *          xenProp.Value = includeValues ? oProps : null;
                     *      }
                     *  }
                     * }
                     */
                }

                result.Add(xenProp);
            }

            return(result.ToArray());
        }