Пример #1
0
        public static Type GetTypeForSchema(this ISchemaTypeMap typeMap, Schema schema)
        {
            Type type;

            if (!typeMap.TryGetTypeForSchema(schema, out type))
            {
                throw new MissingTypeMappingException(schema);
            }
            return(type);
        }
Пример #2
0
        public void ApplyToObject(object target, ICursor source)
        {
            var type   = target.GetType();
            var schema = source.Schema;

            if (schema.DataType == DataType.Class)
            {
                var fields = type.GetTypeInfo().GetFields(BindingFlags.Public | BindingFlags.Instance);
                foreach (var f in fields)
                {
                    object value;
                    if (TryGetValueForProperty(source, f.Name, f.FieldType, out value))
                    {
                        f.SetValue(target, value);
                    }
                }

                var properties = type.GetTypeInfo().GetProperties(BindingFlags.Public | BindingFlags.Instance);
                foreach (var p in properties.Where(x => x.CanWrite))
                {
                    object value;
                    if (TryGetValueForProperty(source, p.Name, p.PropertyType, out value))
                    {
                        var setter = p.GetSetMethod();
                        setter.Invoke(target, new [] { value });
                    }
                }

                if (schema.Id == (int)BuiltInSchema.Item &&
                    (!type.GetTypeInfo().IsGenericType || type.GetGenericTypeDefinition() != typeof(Item <>)) &&
                    typeof(Item).GetTypeInfo().IsAssignableFrom(type))
                {
                    ICursor propertyCursor = source.GoTo((int)ItemLayout.Data, true);
                    ApplyToObject(target, propertyCursor);
                }
            }
            else if (schema.DataType == DataType.List || schema.DataType == DataType.MultiChoice)
            {
                if (type.IsArray)
                {
                    int i           = 0;
                    var targetArray = (Array)target;
                    foreach (var v in source.Children.Select(FromCursor))
                    {
                        targetArray.SetValue(v, i++);
                    }
                }
                else
                {
                    Schema itemSchema;
                    if (schema.DataType == DataType.MultiChoice)
                    {
                        itemSchema = Schema.BuiltIn[BuiltInSchema.Int32];
                    }
                    else
                    {
                        itemSchema = schema[0].Schema;
                    }

                    if (itemSchema.Id == (int)BuiltInSchema.Variable)
                    {
                        foreach (var itemCursor in source.Children)
                        {
                            object value = this.FromCursor(itemCursor);
                            if (value == null)
                            {
                                continue;
                            }

                            var addMethod = type.GetTypeInfo().GetMethod("Add", new Type[] { value.GetType() });
                            if (addMethod != null)
                            {
                                addMethod.Invoke(target, new object[] { value });
                            }
                        }
                    }
                    else
                    {
                        Type itemType;
                        if (schemaTypeMap.TryGetTypeForSchema(itemSchema, out itemType))
                        {
                            var addMethod = type.GetTypeInfo().GetMethod("Add", new Type[] { itemType });
                            if (addMethod == null)
                            {
                                return;
                            }

                            foreach (var itemCursor in source.Children)
                            {
                                object value = this.FromCursor(itemCursor);

                                if (!itemType.GetTypeInfo().IsAssignableFrom(value.GetType()))
                                {
                                    value = Convert.ChangeType(value, itemType);
                                }

                                addMethod.Invoke(target, new object[] { value });
                            }
                        }
                    }
                }
            }
        }