Esempio n. 1
0
        public override bool GetNullable(ObjectMapper mapper, MemberAccessor member, out bool isSet)
        {
            // Check member [Nullable(true | false)]
            //
            NullableAttribute attr1 = member.GetAttribute <NullableAttribute>();

            if (attr1 != null)
            {
                isSet = true;
                return(attr1.IsNullable);
            }

            // Check member [NullValue(0)]
            //
            NullValueAttribute attr2 = member.GetAttribute <NullValueAttribute>();

            if (attr2 != null)
            {
                return(isSet = true);
            }

            // Check type [Nullable(true || false)]
            //
            attr1 = (NullableAttribute)TypeHelper.GetFirstAttribute(
                member.MemberInfo.DeclaringType, typeof(NullableAttribute));

            if (attr1 != null)
            {
                isSet = true;
                return(attr1.IsNullable);
            }

            // Check type [NullValues(typeof(int), 0)]
            //
            object[] attrs = member.GetTypeAttributes(typeof(NullValueAttribute));

            foreach (NullValueAttribute a in attrs)
            {
                if (a.Type == null && a.Value != null && a.Value.GetType() == member.Type ||
                    a.Type != null && a.Type == member.Type)
                {
                    return(isSet = true);
                }
            }

            if (member.Type.IsEnum)
            {
                return(isSet = mapper.MappingSchema.GetNullValue(member.Type) != null);
            }

            return(base.GetNullable(mapper, member, out isSet));
        }
Esempio n. 2
0
        public static string GetDescription(this NullableAttribute attr)
        {
            if (attr == null)
            {
                return("no attribute");
            }
            if (attr.Modes != null)
            {
                return("[" + string.Join(", ", attr.Modes.Select(x => x.GetDescription())) + "]");
            }

            return(attr.Mode.GetDescription());
        }
Esempio n. 3
0
        public static bool?GetNullable(this NullableAttribute attr, int position = 0)
        {
            if (position > 0 && attr.NullableFlags.Length == 1)
            {
                position = 0;
            }

            var first = attr.NullableFlags[position];

            return(first == 1 ? (bool?)false :
                   first == 2 ? (bool?)true :
                   null);
        }