Example #1
0
        public List <EnumDesc> GetPropEnums(object obj, ExampleDbContext context)
        {
            var list   = context.Books.ToList();
            var retval = list.Select(x => new EnumDesc()
            {
                text = $"{x.Writer}: {x.Name}", value = x.Id
            }).ToList();

            return(retval);
        }
Example #2
0
        public List <EnumDesc> GetPropEnums(object obj, ExampleDbContext context)
        {
            List <EnumDesc> retval    = new List <EnumDesc>();
            var             endYear   = DateTime.Now.Year;
            var             startYear = 1500;

            if (obj is MovieDTO movie)
            {
                startYear = 1900;
            }
            for (int i = endYear; i >= startYear; i--)
            {
                retval.Add(new EnumDesc {
                    text = $"{i}", value = i
                });
            }
            return(retval);
        }
Example #3
0
        public Schema(Object obj, ExampleDbContext context)
        {
            this.ClassName = obj.GetType().Name;
            this.Display   = this.ClassName;

            System.Reflection.MemberInfo info = obj.GetType();
            object[] attributes = info.GetCustomAttributes(true);

            foreach (object attr in attributes)
            {
                if (attr is DisplayAttribute x)
                {
                    this.Display = x.Name;
                }
            }

            PropertyInfo[] props = obj.GetType().GetProperties();
            foreach (PropertyInfo prop in props)
            {
                Prop pr  = new Prop();
                bool add = true;

                if (prop.CanWrite == false)
                {
                    pr.Readonly = true;
                }
                pr.Name  = prop.Name;
                pr.Title = prop.Name;
                if (pr.Name == $"{this.ClassName}Id" || pr.Name == "Id")
                {
                    this.KeyName = pr.Name;
                }

                pr.Type      = GetType(prop.PropertyType);
                pr.InputType = pr.Type; //initial state, DataTypeAttribute may change this
                pr.Required  = GetRequired(prop.PropertyType);
                pr.Max       = GetRangeMax(prop.PropertyType);
                pr.Min       = GetRangeMin(prop.PropertyType);
                pr.PropEnums = GetDescriptions(prop.PropertyType);

                object[] attrs = prop.GetCustomAttributes(true);
                foreach (object attr in attrs)
                {
                    if (attr is DisplayAttribute da)
                    {
                        pr.Title = da.Name;
                    }
                    else if (attr is DataTypeAttribute dta)
                    {
                        pr.InputType = ToInputType(dta.DataType);
                    }
                    else if (attr is HiddenAttribute h)
                    {
                        pr.Hidden = true;
                    }
                    else if (attr is ReadOnlyAttribute r)
                    {
                        pr.Readonly = r.IsReadOnly;
                    }
                    else if (attr is JsonIgnoreAttribute j)
                    {
                        add = false;
                    }
                    else if (attr is OptionsSourceAttribute o)
                    {
                        Type            magicType        = Type.GetType(o.ClassName);
                        ConstructorInfo magicConstructor = magicType.GetConstructor(Type.EmptyTypes);
                        object          magicClassObject = magicConstructor.Invoke(new object[] { });
                        MethodInfo      magicMethod      = magicType.GetMethod("GetPropEnums");
                        object          magicValue       = magicMethod.Invoke(magicClassObject, new object[] { obj, context });
                        pr.PropEnums = magicValue as List <EnumDesc>;
                        pr.Type      = "enum";
                    }
                    else if (attr is RequiredAttribute re)
                    {
                        pr.Required = true;
                    }
                    else if (attr is RangeAttribute ra)
                    {
                        pr.Min = ra.Minimum;
                        pr.Max = ra.Maximum;
                    }
                    else if (attr is StringLengthAttribute sl)
                    {
                        pr.MaxLength = sl.MaximumLength;
                        pr.MinLength = sl.MinimumLength;
                    }
                    else if (attr is RegularExpressionAttribute reg)
                    {
                        pr.Pattern = reg.Pattern;
                    }
                }
                if (add == true)
                {
                    this.Props.Add(pr);
                }
            }
        }