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)); } }
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)); } }
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)); }
public static Int32?ToInt32(this DataEnum value) { if (value == null) { return(null); } else { return(value.Id); } }
/// <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); } }
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); } }
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)); } }