示例#1
0
        public static IModel FromType(Type formType)
        {
            var formName = formType.FullName;

            if (formType.GetCustomAttributes(typeof(FormNameAttribute), false).FirstOrDefault() is FormNameAttribute formNameAttribute)
            {
                formName = formNameAttribute.FullName;
            }

            var model = new FormModel(formName)
            {
                Constructor = f =>
                {
                    var serviceProvider = (IServiceProvider)Prop.GetValue(null);

                    var instance = ActivatorUtilities.CreateInstance(serviceProvider, formType);

                    f.FormData.Add("PocoInstance", instance);
                }
            };

            if (typeof(IValidationHandler).IsAssignableFrom(formType))
            {
                model.OnValidateHandler = (f, e) =>
                {
                    var instance = (IValidationHandler)f.FormData["PocoInstance"];

                    instance.OnValidate(e);
                };
            }

            model.SetDefaultValuesHandler = f =>
            {
                var instance = (IPOCOForm)f.FormData["PocoInstance"];

                SetDefaultValues(instance, f);
            };

            foreach (var itm in formType.GetCustomAttributes(true).Cast <Attribute>())
            {
                model.Attributes.Add(itm);
            }

            foreach (var prop in GetFieldProps(formType).Where(p => p.CanRead))
            {
                var attributes = prop.GetCustomAttributes(true).Cast <Attribute>().ToList();
                var field      = new FormFieldModel(model, prop.Name, prop.PropertyType, attributes)
                {
                    IsReadOnly = !prop.CanWrite
                };

                model.Fields.Add(field);
            }

            return(model);
        }
 public static string EvaluateT(FormFieldModel field, string setting, string defaultValue)
 {
     return(EvaluateT(field.OwningForm, field.Name + "." + setting, defaultValue));
 }
示例#3
0
 public FormField(FormFieldModel model, Form instance)
 {
     Model      = model;
     OwningForm = instance;
 }