.NetCore automatically converted to display custom enumeration class Chinese name

Usually we define the enumeration in the class, you may use digital or English, but when they want to display interface display Chinese, I summarized the following two methods

(1) displays the name of the custom enumeration of:

    public enum WorkFlowProcessState
    {
        [Display (the Name = " not started " )]
        None = 0,
        [Display (the Name = " Waiting " )]
        Waiting,
        [Display (the Name = " Processing " )]
        Processing,
        [Display (the Name = " passed " )]
        Passed,
        [Display (the Name = " failed " )]
        UnPassed,
        [Display (the Name = " disabled " )]
        Disabled,
        [Display (the Name = " revoked " )]
        Canceled
    }

I use JsonConverter method Newtonsoft.Json provided to enable the automatic conversion of WebAPI enumeration class in return Json. Add the following method Converter:

public  class JsonEnumDisplayConverter: JsonConverter
    {
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            try
            {
                var t = value.GetType().GetFields().SingleOrDefault(w => w.Name == value.ToString()).CustomAttributes.SingleOrDefault(w => w.AttributeType == typeof(DisplayAttribute));
                if (t != null)
                {
                    writer.WriteValue(t.NamedArguments[0].TypedValue.Value.ToString());
                }
                else
                {
                    writer.WriteValue(value.ToString());

                }
            }
            catch
            {

                writer.WriteValue("");
            }
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
        }

        public override bool CanRead => false;

        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(string);
        }
    }

Defined in the model-based (or view model class):

        [Display(Name = "审批状态"),JsonConverter(typeof(JsonEnumDisplayConverter))]
        public WorkFlowProcessState PROCESS_STATE { get; set; }

Note JsonConverter (typeof (JsonCustomDateTimeConverter)) This property label, it is to tell the framework how to convert.

 

 

(2) from the database and converted int enum values ​​stored enumeration name:

Newtonsoft.Json library provides a default converter, called StringEnumConverter, using a similar method as above, but written JsonConverter (typeof (StringEnumConverter)), the effect is displayed enumeration defined, such as None, Waiting ...... if you directly Chinese define enumeration values, and can also be a method to achieve the same effect.

 

Note that the above method requires a reference packet Newtonsoft.Json

Guess you like

Origin www.cnblogs.com/cdoneiX/p/12239424.html