Is it possible to create a generic Int-to-Enum Converter?

后端 未结 5 808
自闭症患者
自闭症患者 2020-12-19 01:43

I\'d like to be able to say


<
5条回答
  •  旧巷少年郎
    2020-12-19 02:22

    You could also go the other way around and convert the enum to int for the Value using a custom Markup Extension.

    Example

    
    

    EnumToIntExtension

    public class EnumToIntExtension : MarkupExtension
    {
        public object EnumValue
        {
            get;
            set;
        }
        public EnumToIntExtension(object enumValue)
        {
            this.EnumValue = enumValue;
        } 
        public override object ProvideValue(IServiceProvider provider)
        {
            if (EnumValue != null && EnumValue is Enum)
            {
                return System.Convert.ToInt32(EnumValue);
            }
            return -1;
        }
    }
    

提交回复
热议问题