Inheritance: System.Attribute
Example #1
0
        public CompoundInfo(Type type)
        {
            Type = type;

            foreach (PropertyInfo prop in type.GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
            {
                MemberAttribute memberAttribute = prop.GetCustomAttributes(typeof(MemberAttribute), true).Cast <MemberAttribute>().FirstOrDefault();// .NET 4.5: prop.GetCustomAttribute<MemberAttribute>(true);
                if (memberAttribute != null)
                {
                    var meminfo = new MemberInfo(memberAttribute, prop);
                    foreach (string name in memberAttribute.FieldNames)
                    {
                        if (fieldNames.Contains(name))
                        {
                            throw new InvalidProgramException("Duplicate field name: " + name);
                        }

                        fieldNames.Add(name);
                        members.Add(name, meminfo);
                    }
                }
            }
        }
Example #2
0
        public MemberInfo(MemberAttribute attribute, PropertyInfo property)
        {
            this.property = property;

            fieldNames = attribute.FieldNames;

            IsLazy      = property.GetCustomAttributes(typeof(LazyAttribute), false).Length > 0;
            IsFakeFloat = property.GetCustomAttributes(typeof(FakeFloatAttribute), false).Length > 0;
            Initialize  = property.GetCustomAttributes(typeof(InitializeAttribute), false).Length > 0;
            IsRequired  = property.GetCustomAttributes(typeof(RequiredAttribute), false).Length > 0;

            var orderAttribute = (OrderAttribute)property.GetCustomAttributes(typeof(OrderAttribute), false).FirstOrDefault();

            if (orderAttribute != null)
            {
                Order = orderAttribute.Order;
            }

            var localizedStringAttribute = (LocalizedStringAttribute)property.GetCustomAttributes(typeof(LocalizedStringAttribute), false).FirstOrDefault();

            if (localizedStringAttribute != null)
            {
                localizedStringGroup = localizedStringAttribute.LocalizedStringGroup;
            }

            var referenceAttribute = (ReferenceAttribute)property.GetCustomAttributes(typeof(ReferenceAttribute), false).FirstOrDefault();

            if (referenceAttribute != null)
            {
                IsReference         = true;
                ReferencedFormKinds = referenceAttribute.ReferenceFormKinds;
            }

            if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(List <>))
            {
                fieldType  = property.PropertyType.GetGenericArguments()[0];
                IsListType = true;

                addValueToListMethod = typeof(ICollection <>).MakeGenericType(property.PropertyType.GetGenericArguments()).GetMethod("Add");
                createListCtor       = typeof(List <>).MakeGenericType(property.PropertyType.GetGenericArguments()).GetConstructor(paramlessTypes);
            }
            else if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>))
            {
                fieldType      = property.PropertyType.GetGenericArguments()[0];
                IsNullableType = true;
            }
            else
            {
                fieldType  = property.PropertyType;
                IsListType = false;
            }

            IsPrimitiveType = !typeof(Field).IsAssignableFrom(fieldType);

            // Field is dynamic if any of its field names contains a .
            IsDynamic = fieldNames.Where(n => n.Contains('.')).Any();

            if (IsDynamic)
            {
                // Replace . with (.|\n) to cover also new line
                var pattern = string.Join("|", fieldNames).Replace(".", "(.|\n)");
                DynamicArrayRegex = new Regex(pattern);
            }

            //Enums treat enums as if the where the underlaying primitive type.
            if (fieldType.IsEnum)
            {
                fieldType = fieldType.GetEnumUnderlyingType();
            }
        }
Example #3
0
        public MemberInfo(MemberAttribute attribute, PropertyInfo property)
        {
            this.property = property;

            fieldNames = attribute.FieldNames;

            IsLazy = property.GetCustomAttributes(typeof(LazyAttribute), false).Length > 0;
            IsFakeFloat = property.GetCustomAttributes(typeof(FakeFloatAttribute), false).Length > 0;
            Initialize = property.GetCustomAttributes(typeof(InitializeAttribute), false).Length > 0;
            IsRequired = property.GetCustomAttributes(typeof(RequiredAttribute), false).Length > 0;

            var orderAttribute = (OrderAttribute)property.GetCustomAttributes(typeof(OrderAttribute), false).FirstOrDefault();
            if (orderAttribute != null)
            {
                Order = orderAttribute.Order;
            }

            var localizedStringAttribute = (LocalizedStringAttribute)property.GetCustomAttributes(typeof(LocalizedStringAttribute), false).FirstOrDefault();
            if (localizedStringAttribute != null)
            {
                localizedStringGroup = localizedStringAttribute.LocalizedStringGroup;
            }

            var referenceAttribute = (ReferenceAttribute)property.GetCustomAttributes(typeof(ReferenceAttribute), false).FirstOrDefault();
            if (referenceAttribute != null)
            {
                IsReference = true;
                ReferencedFormKinds = referenceAttribute.ReferenceFormKinds;
            }

            if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(List<>))
            {
                fieldType = property.PropertyType.GetGenericArguments()[0];
                IsListType = true;

                addValueToListMethod = typeof(ICollection<>).MakeGenericType(property.PropertyType.GetGenericArguments()).GetMethod("Add");
                createListCtor = typeof(List<>).MakeGenericType(property.PropertyType.GetGenericArguments()).GetConstructor(paramlessTypes);
            }
            else if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
            {
                fieldType = property.PropertyType.GetGenericArguments()[0];
                IsNullableType = true;
            }
            else
            {
                fieldType = property.PropertyType;
                IsListType = false;
            }

            IsPrimitiveType = !typeof(Field).IsAssignableFrom(fieldType);

            // Field is dynamic if any of its field names contains a .
            IsDynamic = fieldNames.Where(n => n.Contains('.')).Any();

            if (IsDynamic)
            {
                // Replace . with (.|\n) to cover also new line
                var pattern = string.Join("|", fieldNames).Replace(".", "(.|\n)");
                DynamicArrayRegex = new Regex(pattern);
            }

            //Enums treat enums as if the where the underlaying primitive type.
            if (fieldType.IsEnum)
            {
                fieldType = fieldType.GetEnumUnderlyingType();
            }
        }