Ejemplo n.º 1
0
        public static FieldInformation ConstructFieldInformation(ModelMember member, object defaultInstance)
        {
            object[] attributes   = member.GetCustomAttributes(false);
            string[] attributeIds = new string[attributes.Length];
            for (int i = 0; i < attributes.Length; i++)
            {
                attributeIds[i] = attributes.GetType().Name;
            }

            FieldInformation fieldInformation;

            if (typeof(InputSocket).IsAssignableFrom(member.ValueType))
            {
                fieldInformation = new FieldInformation()
                {
                    Type         = "InputSocket",
                    Attributes   = attributeIds,
                    DefaultValue = new JValue((object)null),
                    Format       = FieldFormat.Object,
                };
            }
            else
            {
                object defaultValue = null;

                if (defaultInstance != null)
                {
                    defaultValue = member.GetValue(defaultInstance);
                }

                string typeName = member.ValueType.Name;

                try
                {
                    fieldInformation = new FieldInformation
                    {
                        Type         = typeName,
                        Attributes   = attributeIds,
                        DefaultValue = new JValue(defaultValue),
                        Format       = FieldFormat.Object,
                    };
                }
                catch
                {
                    fieldInformation = new FieldInformation
                    {
                        Type         = typeName,
                        Attributes   = attributeIds,
                        DefaultValue = JObject.FromObject(defaultValue),
                        Format       = FieldFormat.Object,
                    };
                }

                if (typeof(IDictionary).IsAssignableFrom(member.ValueType))
                {
                    fieldInformation.Format = FieldFormat.Dictionary;
                    fieldInformation.Type   = member.ValueType.GetGenericArguments()[1].Name;

                    fieldInformation.ValueFormat = new FieldInformation()
                    {
                        Type   = member.ValueType.GetGenericArguments()[1].Name,
                        Format = FieldFormat.Object
                    };
                }
                else if (member.ValueType.IsArray)
                {
                    var elementType = member.ValueType.GetElementType();

                    fieldInformation.Format = FieldFormat.List;
                    fieldInformation.Type   = elementType.Name;

                    fieldInformation.ValueFormat = new FieldInformation()
                    {
                        Type   = elementType.Name,
                        Format = FieldFormat.Object
                    };
                }
                else if (member.ValueType.IsGenericType &&
                         !member.ValueType.IsGenericTypeDefinition &&
                         member.ValueType.GetGenericTypeDefinition() == typeof(List <>))
                {
                    var elementType = member.ValueType.GenericTypeArguments[0];

                    fieldInformation.Format = FieldFormat.List;
                    fieldInformation.Type   = elementType.Name;

                    fieldInformation.ValueFormat = new FieldInformation()
                    {
                        Type   = elementType.Name,
                        Format = FieldFormat.Object
                    };
                }
                else
                {
                    fieldInformation.Format = FieldFormat.Object;
                }
            }

            return(fieldInformation);
        }
Ejemplo n.º 2
0
        public static FieldInformation ConstructFieldInformation(ModelMember member, object defaultInstance)
        {
            // Attributes
            object[] attributes   = member.GetCustomAttributes(false);
            string[] attributeIds = new string[attributes.Length];
            for (int i = 0; i < attributes.Length; i++)
            {
                attributeIds[i] = attributes.GetType().Name;
            }

            // Default Value
            object defaultValue = null;

            if (defaultInstance != null)
            {
                defaultValue = member.GetValue(defaultInstance);
            }
            JToken defaultValueToken;

            try
            {
                defaultValueToken = new JValue(defaultValue);
            }
            catch
            {
                defaultValueToken = JToken.FromObject(defaultValue);
            }

            // Wrappers and inner-most type
            FieldWrapper wrapper        = null;
            FieldWrapper currentWrapper = null;
            var          currentType    = member.ValueType;

            void PushWrapper(FieldWrapperType fieldWrapperType)
            {
                var newWrapper = new FieldWrapper()
                {
                    Type = fieldWrapperType
                };

                if (wrapper == null)
                {
                    wrapper        = newWrapper;
                    currentWrapper = newWrapper;
                }
                else
                {
                    currentWrapper.Child = newWrapper;
                }
            }

            while (true)
            {
                if (typeof(IDictionary).IsAssignableFrom(currentType))
                {
                    currentType = currentType.GetGenericArguments()[1];

                    PushWrapper(FieldWrapperType.Dictionary);
                }
                else if (currentType.IsArray)
                {
                    currentType = currentType.GetElementType();

                    PushWrapper(FieldWrapperType.List);
                }
                else if (currentType.IsGenericType &&
                         !currentType.IsGenericTypeDefinition &&
                         currentType.GetGenericTypeDefinition() == typeof(List <>))
                {
                    currentType = currentType.GenericTypeArguments[0];

                    PushWrapper(FieldWrapperType.List);
                }
                else
                {
                    break;
                }
            }

            return(new FieldInformation
            {
                Name = member.Name,
                Description = null,
                Type = currentType.Name,
                Attributes = attributeIds,
                DefaultValue = defaultValueToken,
                Wrapper = wrapper
            });
        }