/// <summary>
        /// Convert a single property into a derived type of entry using a custom strategy
        /// </summary>
        /// <returns>Converted property</returns>
        public static Entry EncodeProperty(PropertyInfo property, ICustomSerialization customSerialization)
        {
            // Fill with default if entry is null
            var entry = new Entry
            {
                DisplayName = property.GetDisplayName() ?? property.Name,
                Identifier  = property.Name,
                Description = property.GetDescription(),
                Value       = CreateEntryValue(property, customSerialization),
                Validation  = customSerialization.CreateValidation(property.PropertyType, property)
            };

            // No prototypes for readonly entries or entries without flexible sub-entries
            if (entry.Value.IsReadOnly || entry.Value.Type != EntryValueType.Collection && entry.Value.Type != EntryValueType.Class)
            {
                return(entry);
            }

            // Determine and convert prototypes
            var prototypes = Prototypes(property.PropertyType, property, customSerialization);

            entry.Prototypes.AddRange(prototypes);

            return(entry);
        }
        /// <summary>
        /// Create prototypes for possible values of an entry
        /// </summary>
        private static IEnumerable <Entry> Prototypes(Type memberType, ICustomAttributeProvider customAttributeProvider, ICustomSerialization customSerialization)
        {
            var possibleElementValues = IsCollection(memberType)
                ? customSerialization.PossibleElementValues(memberType, customAttributeProvider)
                : customSerialization.PossibleValues(memberType, customAttributeProvider);
            var validation = customSerialization.CreateValidation(memberType, customAttributeProvider);

            foreach (var prototype in customSerialization.Prototypes(memberType, customAttributeProvider))
            {
                var prototypeEntry = Prototype(prototype, customSerialization);
                prototypeEntry.Validation     = validation;
                prototypeEntry.Value.Possible = possibleElementValues;
                yield return(prototypeEntry);
            }
        }
        /// <summary>
        /// Create basic <see cref="Entry"/> instance for a given object type
        /// </summary>
        private static Entry CreateFromType(Type objectType, ICustomSerialization serialization)
        {
            var entry = new Entry
            {
                DisplayName = objectType.GetDisplayName() ?? objectType.Name,
                Identifier  = objectType.Name,
                Value       = new EntryValue
                {
                    Current = objectType.Name,
                    Default = objectType.Name,
                    Type    = TransformType(objectType)
                },
                Validation  = serialization.CreateValidation(objectType, objectType),
                Description = objectType.GetDescription()
            };

            return(entry);
        }
Beispiel #4
0
        /// <summary>
        /// Convert a single property into a derived type of entry using a custom strategy
        /// </summary>
        /// <returns>Covnerted property</returns>
        public static Entry EncodeProperty(PropertyInfo property, ICustomSerialization customSerialization)
        {
            // Fill with default if entry is null
            var entry = new Entry
            {
                DisplayName = property.GetDisplayName() ?? property.Name,
                Identifier  = property.Name,
                Description = property.GetDescription(),
                Value       = CreateEntryValue(property, customSerialization),
                Validation  = customSerialization.CreateValidation(property.PropertyType, property)
            };

            // Include prototypes for collections and classes
            if (entry.Value.Type == EntryValueType.Collection || entry.Value.Type == EntryValueType.Class)
            {
                var prototypes = Prototypes(property.PropertyType, property, customSerialization);
                entry.Prototypes.AddRange(prototypes);
            }

            return(entry);
        }
        /// <summary>
        /// Convert a method parameter to our standard <see cref="Entry"/> format
        /// </summary>
        private static Entry ConvertParameter(ParameterInfo parameter, ICustomSerialization serialization)
        {
            var parameterType = parameter.ParameterType;
            var defaultValue  = parameter.HasDefaultValue ? parameter.DefaultValue.ToString() : null;

            var parameterModel = new Entry
            {
                DisplayName = parameter.GetDisplayName() ?? parameter.Name,
                Identifier  = parameter.Name,
                Description = parameter.GetDescription(),
                Value       = new EntryValue
                {
                    Type     = TransformType(parameter.ParameterType),
                    UnitType = serialization.GetUnitTypeByAttributes(parameter),
                    Current  = defaultValue,
                    Default  = defaultValue,
                    Possible = serialization.PossibleValues(parameterType, parameter)
                },
                Validation = serialization.CreateValidation(parameterType, parameter)
            };

            switch (parameterModel.Value.Type)
            {
            case EntryValueType.Class:
                parameterModel.Value.Current = parameterType.Name;
                parameterModel.Prototypes.AddRange(Prototypes(parameterType, parameter, serialization));
                parameterModel.SubEntries = EncodeClass(parameterType, serialization).SubEntries;
                break;

            case EntryValueType.Collection:
                var elemType = ElementType(parameterType);
                parameterModel.Value.Current = elemType.Name;
                parameterModel.Prototypes.AddRange(Prototypes(parameterType, parameter, serialization));
                break;
            }

            return(parameterModel);
        }