示例#1
0
 public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
 {
     if (int.TryParse((string)value, out int fromInteger))
     {
         return(SmartEnum <TEnumeration, string> .FromValue(fromInteger.ToString()));
     }
     return(SmartEnum <TEnumeration, string> .FromValue((string)value));
 }
        public TEnum Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            if (reader.ReadIsNull())
            {
                return(null);
            }

            return(SmartEnum <TEnum, TValue> .FromValue(ReadValue(ref reader)));
        }
示例#3
0
        public TEnum Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
        {
            if (reader.ReadIsNull())
            {
                return(null);
            }

            var name = reader.ReadString();

            return(SmartEnum <TEnum, TValue> .FromName(name));
        }
示例#4
0
 private TEnum GetFromValue(TValue value)
 {
     try
     {
         return(SmartEnum <TEnum, TValue> .FromValue(value));
     }
     catch (Exception ex)
     {
         throw new JsonException($"Error converting value '{value}' to a smart enum.", ex);
     }
 }
示例#5
0
 private TEnum GetFromName(string name)
 {
     try
     {
         return(SmartEnum <TEnum, TValue> .FromName(name));
     }
     catch (Exception ex)
     {
         throw new JsonException($"Error converting value '{name}' to a smart enum.", ex);
     }
 }
        public TEnum Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
        {
            if (MessagePackBinary.IsNil(bytes, offset))
            {
                readSize = 1;
                return(null);
            }

            var name = MessagePackBinary.ReadString(bytes, offset, out readSize);

            return(SmartEnum <TEnum, TValue> .FromName(name));
        }
        public override TEnum ReadJson(JsonReader reader, Type objectType, TEnum existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            try
            {
                TValue value;
                if (reader.TokenType == JsonToken.Integer && typeof(TValue) != typeof(long) && typeof(TValue) != typeof(bool))
                {
                    value = (TValue)Convert.ChangeType(reader.Value, typeof(TValue));
                }
                else
                {
                    value = (TValue)reader.Value;
                }

                return(SmartEnum <TEnum, TValue> .FromValue(value));
            }
            catch (Exception ex)
            {
                throw new JsonSerializationException($"Error converting {reader.Value ?? "Null"} to {objectType.Name}.", ex);
            }
        }
        public override TEnum ReadJson(JsonReader reader, Type objectType, TEnum existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            switch (reader.TokenType)
            {
            case JsonToken.String:
                return(GetFromName((string)reader.Value));

            default:
                throw new JsonSerializationException($"Unexpected token {reader.TokenType} when parsing a smart enum.");
            }

            TEnum GetFromName(string name)
            {
                try
                {
                    return(SmartEnum <TEnum, TValue> .FromName(name, false));
                }
                catch (Exception ex)
                {
                    throw new JsonSerializationException($"Error converting value '{name}' to a smart enum.", ex);
                }
            }
        }
示例#9
0
 internal SmartEnumWhen(bool stopEvaluating, SmartEnum <TEnum, TValue> smartEnum)
 {
     this.stopEvaluating = stopEvaluating;
     this.smartEnum      = smartEnum;
 }
        public override void WriteJson(JsonWriter writer, SmartEnum <RanksEnumeration, ushort> value, JsonSerializer serializer)
        {
            JToken token = JToken.FromObject(value.Value);

            token.WriteTo(writer);
        }
        public override SmartEnum <RanksEnumeration, ushort> ReadJson(JsonReader reader, Type objectType, SmartEnum <RanksEnumeration, ushort> existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            ushort enumValue = Convert.ToUInt16(reader.Value);

            return(RanksEnumeration.FromValue(enumValue));
        }
示例#12
0
 public static NSString GetConstant(this SmartEnum self)
 {
     return((NSString)"Smart");
 }
示例#13
0
 public void SetSmartEnumValue([BindAs(typeof(SmartEnum), OriginalType = typeof(NSString))] SmartEnum value)
 {
 }
示例#14
0
 internal SmartEnumThen(bool isMatch, bool stopEvaluating, SmartEnum <TEnum, TValue> smartEnum)
 {
     this.isMatch        = isMatch;
     this.smartEnum      = smartEnum;
     this.stopEvaluating = stopEvaluating;
 }
示例#15
0
 /// <summary>
 /// When this instance is one of the specified <see cref="SmartEnum{TEnum, TValue}"/> parameters.
 /// Execute the action in the subsequent call to Then().
 /// </summary>
 /// <param name="smartEnumWhen">A collection of <see cref="SmartEnum{TEnum, TValue}"/> values to compare to this instance.</param>
 /// <returns>A executor object to execute a supplied action.</returns>
 public SmartEnumThen <TEnum, TValue> When(SmartEnum <TEnum, TValue> smartEnumWhen) =>
 new SmartEnumThen <TEnum, TValue>(isMatch: smartEnum.Equals(smartEnumWhen), stopEvaluating: stopEvaluating, smartEnum: smartEnum);
示例#16
0
 public static Option <T> ParseIgnoringCase <T>(string value) where T : SmartEnum <T, int> =>
 SmartEnum <T, int> .TryFromName(value, ignoreCase : true, out var reason) ? Some(reason) : None;
示例#17
0
        public TEnum Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
        {
            var name = reader.ReadString();

            return(SmartEnum <TEnum, TValue> .FromName(name));
        }