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 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)); }
/// <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); } }