Help with enums

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nomad

    Help with enums

    Hi,

    I would like to create a enum which consists of contenttype i.e. text/
    xml and application/x-www-form-urlencoded. Would this be possible?

    Appreciate any suggestions.
  • Marc Gravell

    #2
    Re: Help with enums

    Well, the attribute itself cannot take that form, but you could use an
    attribute to do this; the following is not very optimised (you could
    cache the pairs in a static list/dictionary) - but works for
    illustration:

    Marc

    using System;
    using System.Reflecti on;

    static class Program
    {
    static void Main()
    {
    ContentTypes ct = ContentTypes.Ur lEncoded;
    string mime = ContentType.Get Mime(ct);
    ContentTypes parsed = ContentType.Par se(mime);
    }
    }

    [AttributeUsage( AttributeTarget s.Field, AllowMultiple = false,
    Inherited = true)]
    public sealed class MimeAttribute : Attribute
    {
    public MimeAttribute(s tring contentType)
    {
    ContentType = contentType;
    }
    public string ContentType { get; private set; }
    }
    public enum ContentTypes
    {
    [Mime(@"text/xml")]
    Xml,
    [Mime(@"applicat ion/x-www-form-urlencoded")]
    UrlEncoded
    }
    static class ContentType
    {
    public static ContentTypes Parse(string mime)
    {
    foreach (FieldInfo field in typeof(ContentT ypes).GetFields ())
    {
    MimeAttribute attrib = Attribute.GetCu stomAttribute(f ield,
    typeof(MimeAttr ibute))
    as MimeAttribute;
    if (attrib != null && attrib.ContentT ype == mime)
    {
    return (ContentTypes) field.GetValue( null);
    }
    }
    throw new ArgumentExcepti on("mime");
    }
    public static string GetMime(Content Types contentType)
    {
    FieldInfo field =
    typeof(ContentT ypes).GetField( contentType.ToS tring());
    if (field != null)
    {
    MimeAttribute attrib = Attribute.GetCu stomAttribute(f ield,
    typeof(MimeAttr ibute))
    as MimeAttribute;
    if (attrib != null) return attrib.ContentT ype;
    }
    throw new ArgumentExcepti on("contentType ");
    }
    }

    Comment

    • Marc Gravell

      #3
      Re: Help with enums

      "the attribute itself "

      should have read "the enum itself".

      BTW - it could be done a lot more simply with static string properties
      or string constants:

      public static class ContentTypes
      {
      public const string
      Xml = @"text/xml",
      UrlEncoded = @"applicatio n/x-www-form-urlencoded";
      }

      Marc

      Comment

      • =?Utf-8?B?anAybXNmdA==?=

        #4
        RE: Help with enums

        Enums are wonderful, especially when it comes to understanding your code.

        You can certainly create your own enum. What are your different values?

        Example:
        enum XmlContentType { type1, type2, type3 }

        The trick is: you have to define your own enums to suit your needs/wants.

        "nomad" wrote:
        Hi,
        >
        I would like to create a enum which consists of contenttype i.e. text/
        xml and application/x-www-form-urlencoded. Would this be possible?
        >
        Appreciate any suggestions.
        >

        Comment

        • nomad

          #5
          Re: Help with enums

          On Jun 12, 2:30 pm, jp2msft <jp2m...@discus sions.microsoft .comwrote:
          Enums are wonderful, especially when it comes to understanding your code.
          >
          You can certainly create your own enum. What are your different values?
          >
          Example:
          enum XmlContentType { type1, type2, type3 }
          >
          The trick is: you have to define your own enums to suit your needs/wants.
          >
          "nomad" wrote:
          Hi,
          >
          I would like to create a enum which consists of contenttype i.e. text/
          xml and application/x-www-form-urlencoded. Would this be possible?
          >
          Appreciate any suggestions.
          Marc,

          Thanks very much for your reply. I think by the look of it it would
          be simpler to do public constants.

          Thanks again.

          Comment

          Working...