Beispiel #1
0
        public FieldInfoItem(FieldInfo field, bool allowNullEquivalentValue, String propertyName)
        {
            ParamChecker.AssertParamNotNull(field, "field");
            ParamChecker.AssertParamNotNull(propertyName, "propertyName");
            this.allowNullEquivalentValue = allowNullEquivalentValue;
            this.field    = field;
            DeclaringType = field.DeclaringType;
#if SILVERLIGHT
            if (!field.IsPublic)
            {
                throw new Exception("Field '" + field + "' not public. This is not valid in Silverlight. One possibility is to set the field public or define public setter/getter.");
            }
#endif
            this.propertyName = propertyName;

            Type fieldType = field.FieldType;
            ElementType = TypeInfoItemUtil.GetElementTypeUsingReflection(fieldType, null);
            if (fieldType.IsValueType || fieldType.IsPrimitive)
            {
                NullEquivalentValue = NullEquivalentValueUtil.GetNullEquivalentValue(fieldType);
            }
            Object[] attributes = field.GetCustomAttributes(typeof(XmlElementAttribute), false);
            if (attributes != null && attributes.Length > 0)
            {
                XMLName = ((XmlElementAttribute)attributes[0]).ElementName;
            }
            if (XMLName == null || XMLName.Length == 0)
            {
                XMLName = Name;
            }
            xmlIgnore = false;
#if !SILVERLIGHT
            attributes = field.GetCustomAttributes(typeof(NonSerializedAttribute), false);
            if (attributes != null && attributes.Length > 0)
            {
                xmlIgnore = true;
            }
#endif
            attributes = field.GetCustomAttributes(typeof(IgnoreDataMemberAttribute), false);
            if (attributes != null && attributes.Length > 0)
            {
                xmlIgnore = true;
            }
            attributes = field.GetCustomAttributes(typeof(XmlIgnoreAttribute), false);
            if (attributes != null && attributes.Length > 0)
            {
                xmlIgnore = true;
            }
        }
Beispiel #2
0
        public FieldPropertyInfo(Type entityType, String propertyName, FieldInfo field)
            : base(entityType)
        {
            EntityType   = entityType;
            BackingField = field;
            AddModifiers(field);
            Name          = propertyName;
            DeclaringType = field.DeclaringType;
            PropertyType  = field.FieldType;
            ElementType   = TypeInfoItemUtil.GetElementTypeUsingReflection(PropertyType, null);

            IsWritable = field.IsPublic || field.IsFamily;
            IsReadable = IsWritable;

            Init();
        }
Beispiel #3
0
        public WrappedPropertyInfo(Type entityType, PropertyInfo propertyInfo) : base(entityType)
        {
            EntityType    = entityType;
            Name          = propertyInfo.Name;
            DeclaringType = propertyInfo.DeclaringType;
            PropertyType  = propertyInfo.PropertyType;
            ElementType   = TypeInfoItemUtil.GetElementTypeUsingReflection(PropertyType, null);

            this.propertyInfo = propertyInfo;

            IsReadable = propertyInfo.GetGetMethod() != null?propertyInfo.GetGetMethod().IsPublic || propertyInfo.GetGetMethod().IsFamily : false;

            IsWritable = propertyInfo.GetSetMethod() != null?propertyInfo.GetSetMethod().IsPublic || propertyInfo.GetSetMethod().IsFamily : false;

            Init();
        }
Beispiel #4
0
        public PropertyInfoItem(IPropertyInfo property, bool allowNullEquivalentValue)
        {
            this.property = (IPropertyInfoIntern)property;
            this.AllowNullEquivalentValue = allowNullEquivalentValue;
            Type propertyType = property.PropertyType;

            DeclaringType = property.DeclaringType;
            ElementType   = TypeInfoItemUtil.GetElementTypeUsingReflection(propertyType, null);

            if (propertyType.IsValueType || propertyType.IsPrimitive)
            {
                NullEquivalentValue = NullEquivalentValueUtil.GetNullEquivalentValue(propertyType);
            }
            XmlElementAttribute annotation = property.GetAnnotation <XmlElementAttribute>();

            if (annotation != null)
            {
                XMLName = annotation.ElementName;
            }
            if (XMLName == null || XMLName.Length == 0)
            {
                XMLName = Name;
            }
            xmlIgnore = false;
#if !SILVERLIGHT
            if (property.GetAnnotation <NonSerializedAttribute>() != null)
            {
                xmlIgnore = true;
            }
#endif
            if (property.GetAnnotation <IgnoreDataMemberAttribute>() != null)
            {
                xmlIgnore = true;
            }
            if (property.GetAnnotation <XmlIgnoreAttribute>() != null)
            {
                xmlIgnore = true;
            }
            if (!CanRead || !CanWrite)
            {
                xmlIgnore = true;
            }
        }
Beispiel #5
0
        protected override void Init()
        {
            if (EntityType == null)
            {
                throw new ArgumentException("No class given");
            }
            if (Getter == null && Setter == null)
            {
                throw new ArgumentException("No property methods (class is '" + EntityType + "')");
            }
            else if (Getter != null)
            {
                Type declaringClass = Getter.DeclaringType;
                PropertyType = Getter.ReturnType;
                ElementType  = TypeInfoItemUtil.GetElementTypeUsingReflection(PropertyType, null);

                String      nameLower        = Name.ToLowerInvariant();
                FieldInfo[] fields           = ReflectUtil.GetDeclaredFieldsInHierarchy(declaringClass);
                FieldInfo   backingField     = null;
                FieldInfo   weakBackingField = null;
                String      backingFieldName = "<" + Name + ">k__BackingField";

                for (int a = fields.Length; a-- > 0;)
                {
                    FieldInfo field = fields[a];
                    if (field.IsStatic)
                    {
                        continue;
                    }
                    String fieldName = field.Name.ToLowerInvariant();
                    if (fieldName.Equals(nameLower) || field.Name.Equals(backingFieldName))
                    {
                        backingField = field;
                        break;
                    }
                    else if (fieldName.EndsWith(nameLower))
                    {
                        if (weakBackingField != null)
                        {
                            weakBackingField = null;
                            break;
                        }
                        weakBackingField = field;
                    }
                }
                if (backingField == null)
                {
                    backingField = weakBackingField;
                }
                BackingField = backingField;
                if (backingField != null)
                {
                    AddModifiers(backingField);
                    PutAnnotations(backingField);
                }
                PutAnnotations(Getter);
                if (Setter != null)
                {
                    if (Setter.GetParameters().Length != 1 || !PropertyType.IsAssignableFrom(Setter.GetParameters()[0].ParameterType))
                    {
                        throw new Exception("Misfitting property methods for property '" + Name + "' on class '" + EntityType.Name + "'");
                    }
                    PutAnnotations(Setter);
                }
            }
            else
            {
                Type        declaringClass = Setter.DeclaringType;
                FieldInfo[] fields         = ReflectUtil.GetDeclaredFieldsInHierarchy(declaringClass);
                for (int a = fields.Length; a-- > 0;)
                {
                    FieldInfo field = fields[a];
                    if (field.IsInitOnly || field.IsStatic)
                    {
                        continue;
                    }
                    String fieldName = field.Name;
                    if (fieldName.EndsWith(Name))
                    {
                        if (BackingField != null)
                        {
                            BackingField = null;
                            break;
                        }
                        BackingField = field;
                    }
                }
                if (BackingField != null)
                {
                    AddModifiers(BackingField);
                }
                PropertyType = Setter.GetParameters()[0].ParameterType;
                ElementType  = TypeInfoItemUtil.GetElementTypeUsingReflection(PropertyType, null);
                PutAnnotations(Setter);
            }
            //if (Getter != null && Modifier.isNative(Getter.getModifiers()))
            //{
            //	modifiers |= Modifier.NATIVE;
            //}
            //if (Setter != null && Modifier.isNative(Setter.getModifiers()))
            //{
            //	modifiers |= Modifier.NATIVE;
            //}
            if (Setter != null)
            {
                UpdateModifiers(false, (int)Modifier.FINAL);
            }
            bool?getterIsPublic = Getter != null ? (bool?)(Getter.IsPublic) : null;
            bool?setterIsPublic = Setter != null ? (bool?)(Setter.IsPublic) : null;

            if (getterIsPublic == true || setterIsPublic == true)
            {
                UpdateModifiers(false, (int)Modifier.PRIVATE);
                UpdateModifiers(false, (int)Modifier.PROTECTED);
                UpdateModifiers(true, (int)Modifier.PUBLIC);
            }
            else
            {
                bool?GetterIsProtected = Getter != null ? (bool?)(Getter.IsFamily || Getter.IsFamilyOrAssembly) : null;
                bool?SetterIsProtected = Setter != null ? (bool?)(Setter.IsFamily || Setter.IsFamilyOrAssembly) : null;
                if (GetterIsProtected == true || SetterIsProtected == true)
                {
                    UpdateModifiers(false, (int)Modifier.PRIVATE);
                    UpdateModifiers(false, (int)Modifier.PUBLIC);
                    UpdateModifiers(true, (int)Modifier.PROTECTED);
                }
                else
                {
                    UpdateModifiers(false, (int)Modifier.PROTECTED);
                    UpdateModifiers(false, (int)Modifier.PUBLIC);
                    UpdateModifiers(true, (int)Modifier.PRIVATE);
                }
            }
            RefreshDeclaringType();
            base.Init();
        }