Help with enums

  • Thread starter Thread starter nomad
  • Start date Start date
N

nomad

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.
 
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.Reflection;

static class Program
{
static void Main()
{
ContentTypes ct = ContentTypes.UrlEncoded;
string mime = ContentType.GetMime(ct);
ContentTypes parsed = ContentType.Parse(mime);
}
}

[AttributeUsage(AttributeTargets.Field, AllowMultiple = false,
Inherited = true)]
public sealed class MimeAttribute : Attribute
{
public MimeAttribute(string contentType)
{
ContentType = contentType;
}
public string ContentType { get; private set; }
}
public enum ContentTypes
{
[Mime(@"text/xml")]
Xml,
[Mime(@"application/x-www-form-urlencoded")]
UrlEncoded
}
static class ContentType
{
public static ContentTypes Parse(string mime)
{
foreach (FieldInfo field in typeof(ContentTypes).GetFields())
{
MimeAttribute attrib = Attribute.GetCustomAttribute(field,
typeof(MimeAttribute))
as MimeAttribute;
if (attrib != null && attrib.ContentType == mime)
{
return (ContentTypes) field.GetValue(null);
}
}
throw new ArgumentException("mime");
}
public static string GetMime(ContentTypes contentType)
{
FieldInfo field =
typeof(ContentTypes).GetField(contentType.ToString());
if (field != null)
{
MimeAttribute attrib = Attribute.GetCustomAttribute(field,
typeof(MimeAttribute))
as MimeAttribute;
if (attrib != null) return attrib.ContentType;
}
throw new ArgumentException("contentType");
}
}
 
"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 = @"application/x-www-form-urlencoded";
}

Marc
 
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.
 
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.

Marc,

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

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top