Ejemplo n.º 1
0
        public void DynamicFill(IEnumerable <IFormTreeNode> nodes, IPropertyValueConverter converter)
        {
            var objectPopulator = new ObjectPopulator(converter);

            foreach (var node in nodes)
            {
                var      propValue = (TKey)converter.ConvertToPropertyType(typeof(TKey), node.Key);
                TElement element;
                if (elements == null || !HasKey(propValue))
                {
                    element = Activator.CreateInstance <TElement>();
                    Add(element);
                    var fieldName = MemberHelpers.GetKeyPropertyFieldName <TElement>();
                    var(field, typeOfField) = MemberHelpers.GetPropertyField(element, fieldName);
                    // Convert property to field, for support readonly model
                    object fieldValue = converter.ConvertToFieldType(typeOfField, propValue);
                    element.SetValue(field, fieldValue);
                }
                else
                {
                    element = this[propValue];
                }
                if (node is FormTreeCollection formTreeCollection)
                {
                    objectPopulator.Populate(formTreeCollection.Childs, element);
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
        }
Ejemplo n.º 2
0
        private bool AutoSetValueMember(object obj, string fieldName, MemberInfo member, Type memberType, string dataValue, IPropertyValueConverter converter)
        {
            // If used read-only model, with other type fields, need use the original type
            Type originalPropType = obj.GetType().GetProperty(fieldName)?.PropertyType ?? member.DeclaringType;
            // This convertation release for check and normalize value from formcollection to property
            object propValue = converter.ConvertToPropertyType(originalPropType, dataValue);
            // Convert property to field
            object fieldValue = Converter.ConvertToFieldType(memberType, propValue);

            // Set new field value
            obj.SetValue(member, fieldValue);

            return(true);
        }