예제 #1
0
        internal static Int64 ConvertEnumFromString(Type enumType, string s)
        {
            if (enumType.IsEnum)
            {
                Int64 v;
                if (Int64.TryParse(s, out v))
                {
                    if (!Enum.IsDefined(enumType, v))
                    {
                        throw new InvalidCastException(String.Format("{0} geçerli bir {1} deðeri deðil!", v, enumType.Name));
                    }

                    return(v);
                }
                else
                {
                    return(Convert.ToInt64(Enum.Parse(enumType, s), CultureInfo.InvariantCulture));
                }
            }
            else if (enumType.IsSubclassOf(typeof(DataEnum)))
            {
                return(((DataEnum)DataEnum.ConvertFromString(enumType, s)).Id);
            }
            else
            {
                throw new InvalidProgramException(String.Format("{0} geçerli bir enum tipi deðil!", enumType.Name));
            }
        }
예제 #2
0
        public override void ValueToJson(JsonWriter writer, Row row, JsonSerializer serializer)
        {
            var value = _getValue(row);

            if (value == null)
            {
                writer.WriteNull();
            }
            else if (EnumType == null)
            {
                writer.WriteValue(value.Value);
            }
            else if (EnumType.IsEnum)
            {
                writer.WriteValue(Enum.GetName(EnumType, value.Value));
            }
            else if (EnumType.IsSubclassOf(typeof(DataEnum)))
            {
                writer.WriteValue(DataEnum.ConvertFromInt32(EnumType, value.Value).Key);
            }
            else
            {
                throw new InvalidProgramException(String.Format("{0} geçerli bir enum tipi deðil!", EnumType.Name));
            }
        }
예제 #3
0
 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
 {
     if (value is string || value is Int32 || value is Int64 || value is Int16)
     {
         return(DataEnum.ConvertFromString(typeof(T), value.ToString()));
     }
     return(base.ConvertFrom(context, culture, value));
 }
예제 #4
0
 public static Int32?ToInt32(this DataEnum value)
 {
     if (value == null)
     {
         return(null);
     }
     else
     {
         return(value.Id);
     }
 }
예제 #5
0
        /// <summary>
        ///   Reads the JSON representation of the object.</summary>
        /// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
        /// <param name="objectType">
        ///   Type of the object.</param>
        /// <param name="existingValue">
        ///   The existing value of object being read.</param>
        /// <param name="serializer">
        ///   The calling serializer.</param>
        /// <returns>
        ///   The object value.</returns>
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            switch (reader.TokenType)
            {
            case JsonToken.Null:
            case JsonToken.Undefined:
                return(null);

            case JsonToken.Integer:
            case JsonToken.Float:
            case JsonToken.Boolean:
                return(DataEnum.ConvertFromInt32(objectType, Convert.ToInt32(reader.Value, CultureInfo.InvariantCulture)));

            case JsonToken.String:
                return(DataEnum.ConvertFromString(objectType, (string)reader.Value));

            default:
                throw new JsonSerializationException("Unexpected token when deserializing row: " + reader.TokenType);
            }
        }
예제 #6
0
        public override void ValueToJson(JsonWriter writer, Row row, JsonSerializer serializer)
        {
            var value = _getValue(row);

            if (value == null)
            {
                writer.WriteNull();
            }
            else if (EnumType == null)
            {
                writer.WriteValue(value.Value);
            }
            else if (EnumType.IsEnum)
            {
                writer.WriteValue(Enum.GetName(EnumType, value.Value));
            }
            else if (EnumType.IsSubclassOf(typeof(DataEnum)))
            {
                writer.WriteValue(DataEnum.ConvertFromInt32(EnumType, (Int32)value.Value).Key);
            }
        }
예제 #7
0
        internal static Int64 ConvertEnumFromInt(Type enumType, Int64 v)
        {
            if (enumType.IsEnum)
            {
                if (!Enum.IsDefined(enumType, v))
                {
                    throw new InvalidCastException(String.Format("{0} geçerli bir {1} deðeri deðil!", v, enumType.Name));
                }

                return(v);
            }
            else if (enumType.IsSubclassOf(typeof(DataEnum)))
            {
                DataEnum.ConvertFromInt32(enumType, (Int32)v);
                return(v);
            }
            else
            {
                throw new InvalidProgramException(String.Format("{0} geçerli bir enum tipi deðil!", enumType.Name));
            }
        }