Exemplo n.º 1
0
        public static IEnumerable <FieldMetadata> GetClassFieldMetadata(Type configTypeName, IDictionary <string, Type> objectTypeMaps = null)
        {
            var result = new List <FieldMetadata>();

            var properties = configTypeName.GetProperties();

            foreach (var item in properties)
            {
                result.Add(FieldMetadata.Create(item, objectTypeMaps));
            }

            if (configTypeName.IsInterface)
            {
                var interfaces = configTypeName.GetInterfaces();
                foreach (var interface_ in interfaces)
                {
                    foreach (var item in interface_.GetProperties())
                    {
                        if (!result.Any(m => m.SchemaName == item.Name))
                        {
                            result.Add(FieldMetadata.Create(item, objectTypeMaps));
                        }
                    }
                }
            }
            else
            {
                if (configTypeName.BaseType != null)
                {
                    foreach (var item in configTypeName.BaseType.GetProperties())
                    {
                        if (!result.Any(m => m.SchemaName == item.Name))
                        {
                            result.Add(FieldMetadata.Create(item, objectTypeMaps));
                        }
                    }
                }
            }

            return(result);
        }
        public static FieldMetadata Create(PropertyInfo propertyInfo, IDictionary <string, Type> objectTypeMaps = null)
        {
            var recordType     = propertyInfo.ReflectedType != null ? propertyInfo.ReflectedType.Name : null;
            var type           = propertyInfo.PropertyType;
            var isNullableType = type.Name == "Nullable`1";

            if (isNullableType)
            {
                type = type.GetGenericArguments()[0];
            }
            var internalName = propertyInfo.Name;
            var label        = propertyInfo.GetDisplayName();

            FieldMetadata fm = null;

            if (type == typeof(bool))
            {
                fm = new BooleanFieldMetadata(recordType, internalName, label);
            }
            else if (type.IsEnum)
            {
                var options = new List <PicklistOption>();
                foreach (Enum item in type.GetEnumValues())
                {
                    options.Add(new PicklistOption(item.ToString(), item.GetDisplayString()));
                }
                fm = new PicklistFieldMetadata(recordType, internalName, label, options);
            }
            else if (type == typeof(Password))
            {
                fm = new PasswordFieldMetadata(recordType, internalName, label);
            }
            else if (type == typeof(Folder))
            {
                fm = new FolderFieldMetadata(recordType, internalName, label);
            }
            else if (type == typeof(string))
            {
                fm           = new StringFieldMetadata(recordType, internalName, label);
                fm.MaxLength = int.MaxValue;
                if (propertyInfo.GetCustomAttribute <Multiline>() != null)
                {
                    fm.TextFormat = TextFormat.TextArea;
                }
            }
            else if (type == typeof(IEnumerable <string>))
            {
                fm = new StringEnumerableFieldMetadata(recordType, internalName, label);
            }
            else if (type == typeof(int))
            {
                fm = new IntegerFieldMetadata(recordType, internalName, label)
                {
                    NotNullable = !isNullableType
                };
                var minimumAttribute = propertyInfo.GetCustomAttribute <MinimumIntValue>();
                if (minimumAttribute != null)
                {
                    fm.MinValue = minimumAttribute.Value;
                }
                var maximumAttribute = propertyInfo.GetCustomAttribute <MaximumIntValue>();
                if (maximumAttribute != null)
                {
                    fm.MaxValue = maximumAttribute.Value;
                }
            }
            else if (type == typeof(Lookup))
            {
                fm = new LookupFieldMetadata(recordType, internalName, label, null);
            }
            else if (type == typeof(RecordType))
            {
                fm = new RecordTypeFieldMetadata(internalName, label);
            }
            else if (type == typeof(RecordField))
            {
                fm = new RecordFieldFieldMetadata(internalName, label);
            }
            else if (type == typeof(FileReference))
            {
                fm = new FileRefFieldMetadata(internalName, label);
            }
            else if (type == typeof(DateTime))
            {
                fm = new DateFieldMetadata(internalName, label);
            }
            else if (type.IsIEnumerableOfT())
            {
                var enumeratedType = type.GetGenericArguments()[0];
                if (enumeratedType.IsEnum)
                {
                    var options = new List <PicklistOption>();
                    foreach (Enum item in enumeratedType.GetEnumValues())
                    {
                        options.Add(new PicklistOption(item.ToString(), item.GetDisplayString()));
                    }
                    fm = new PicklistFieldMetadata(recordType, internalName, label, options)
                    {
                        IsMultiSelect = true
                    };
                }
                else if (enumeratedType == typeof(RecordField))
                {
                    fm = new RecordFieldFieldMetadata(internalName, label)
                    {
                        IsMultiSelect = true
                    };
                }
                else if (objectTypeMaps != null && objectTypeMaps.ContainsKey(internalName))
                {
                    fm = new EnumerableFieldMetadata(internalName, label, objectTypeMaps[internalName].AssemblyQualifiedName);
                }
                else
                {
                    fm = new EnumerableFieldMetadata(internalName, label, enumeratedType.AssemblyQualifiedName);
                }
            }
            else if (type == typeof(decimal))
            {
                fm = new DecimalFieldMetadata(internalName, label);
            }
            else if (type == typeof(Url))
            {
                fm = new UrlFieldMetadata(internalName, label);
            }
            else if (type == typeof(Money))
            {
                fm = new MoneyFieldMetadata(recordType, internalName, label);
            }
            else if (type == typeof(long))
            {
                fm = new BigIntFieldMetadata(recordType, internalName, label);
            }
            else if (type == typeof(double))
            {
                fm = new DoubleFieldMetadata(recordType, internalName, label);
            }
            else
            {
                fm = new ObjectFieldMetadata(recordType, internalName, label, propertyInfo.ReflectedType);
            }
            if (fm == null)
            {
                throw new ArgumentOutOfRangeException(type + " not implemented");
            }
            fm.IsMandatory = true;
            fm.Readable    = propertyInfo.GetGetMethod() != null;
            fm.Createable  = propertyInfo.GetSetMethod() != null;
            fm.Writeable   = propertyInfo.GetSetMethod() != null;
            fm.Searchable  = propertyInfo.GetCustomAttribute <NotSearchable>() == null;
            var orderAttribute = propertyInfo.GetCustomAttribute <DisplayOrder>();

            if (orderAttribute != null)
            {
                fm.Order = orderAttribute.Order;
            }
            else
            {
                fm.Order = 100000;
            }
            var descriptionAttribute = propertyInfo.GetCustomAttribute <MyDescription>();

            if (descriptionAttribute != null)
            {
                fm.Description = descriptionAttribute.Text;
            }
            return(fm);
        }