Using the attribute + extension method to complete the information mapping of the field in the enum

Source: Internet
Author: User

attribute can be used for information mapping, more flexible and decoupled than dictionary or Tuple,attribute, and can correspond to many types

The following is an information map of the field in an enum


1. Create field attribute


[AttributeUsage (Attributetargets.field)]    public class Jobactionmetadatattribute:attribute    {public        jobactionuitype uitype {get; private set;}        public string Label {get; private set;}        Public Jobactionmetadatattribute (Jobactionuitype uitype, string label)        {            uitype = Uitype;            label = label;        }    }



2. Prepare a extension method


  public static class Jobactionextenssion {public static jobactionmetadata GetMetaData (this jobaction jobaction ) {var attr = Attribute.GetCustomAttribute (Jobaction.gettype ().            GetMember (Jobaction.tostring ()) [0], typeof (Jobactionmetadatattribute)); if (attr! = null) {var props = attr. GetType ().                GetProperties ();                Jobactionuitype Uitype; var uitypeobj = props.                FirstOrDefault (p = P.propertytype = = typeof (Jobactionuitype)); if (uitypeobj = = null) {throw new Invaliddataexception (string.                Format ("Failed to find Jobactionuitype property in Jobactionmetadataattribute")); } var uitypestr = Uitypeobj.getvalue (attr, NULL).                ToString (); if (! Enum.tryparse (Uitypestr, True, out Uitype)) {throw new invalidenumargumentexception (str Ing. Format ("Enum parsing failed. StrinG: {0} ", uitypestr)); } var label = props. First (p = = P.name = = "Label"). GetValue (attr, NULL).                ToString ();            return new Jobactionmetadata (Jobaction, Uitype, Label, ""); } Throw new Invaliddataexception (string.        Format ("Failed to find Jobactionmetadatattribute for Action: {0}", jobaction)); }    }



3. Applying attributes on the field of an enum


Public enum jobaction    {        [Jobactionmetadatattribute (jobactionuitype.success, "Paypal Payment")]        makepaypalsimplepayment = 0,        [Jobactionmetadatattribute (Jobactionuitype.success, "pay by Free Pass")]        Makefreepasspayment = 1,        [Jobactionmetadatattribute (Jobactionuitype.success, "pay by company credits")]        Makecompanycreditpayment = 2,    }



4. Use

Job. Jobaction.getmetadata ();


Use attribute + extension method to complete information mapping of field in enum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.