コード例 #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));
 }
コード例 #2
0
        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);
     }
 }
コード例 #6
0
        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));
        }
コード例 #7
0
        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);
            }
        }
コード例 #8
0
        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;
 }
コード例 #10
0
        public override void WriteJson(JsonWriter writer, SmartEnum <RanksEnumeration, ushort> value, JsonSerializer serializer)
        {
            JToken token = JToken.FromObject(value.Value);

            token.WriteTo(writer);
        }
コード例 #11
0
        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
ファイル: SmartEnumThen.cs プロジェクト: wn-forks/SmartEnum
 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));
        }