Ejemplo n.º 1
0
        /// <summary>
        /// Maps the <see cref="SciterValue"/> to the specified <see cref="Type"/>
        /// </summary>
        /// <param name="sciterValue"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentOutOfRangeException"></exception>
        public static object MapTo(this SciterValue sciterValue, Type type)
        {
            if (sciterValue.IsObjectObject)
            {
                sciterValue = sciterValue.Isolate();
            }

            if (!sciterValue.IsMap)
            {
                throw new ArgumentOutOfRangeException(nameof(sciterValue), $"{nameof(sciterValue)} cannot be converted to Map");
            }

            var result = Activator.CreateInstance(type);

            foreach (var property in type.GetProperties())
            {
                var propertyName = property.GetCustomAttribute <SciterPropertyName>()?.PropertyName ?? property.Name;

                if (!sciterValue.TryGetItemInternal(out var value, propertyName) || value.IsUndefined)
                {
                    sciterValue.TryGetItemInternal(out value, property.Name);
                }

                if (value.IsObject || value.IsObjectObject || value.IsMap)
                {
                    property.SetValue(result, value.MapTo(property.PropertyType));
                    continue;
                }

                property.SetValue(result, System.Convert.ChangeType(value.ToObject(), property.PropertyType));
            }

            return(result);
        }
Ejemplo n.º 2
0
 public static bool TryGetItem(this SciterValue sciterValue, out SciterValue value, string key)
 {
     value = SciterValue.Null;
     return(sciterValue?.TryGetItemInternal(value: out value, key: key) == true);
 }
Ejemplo n.º 3
0
 public static bool TryGetItem(this SciterValue sciterValue, out SciterValue value, int index)
 {
     value = SciterValue.Null;
     return(sciterValue?.TryGetItemInternal(value: out value, index: index) == true);
 }