473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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.
Jun 27 '08 #1
4 1490
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");
}
}
Jun 27 '08 #2
"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
Jun 27 '08 #3
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.
Jun 27 '08 #4
On Jun 12, 2:30 pm, jp2msft <jp2m...@discussions.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.
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
2
by: SpotNet | last post by:
Hello CSharpies, Can Enums in C# be UInt32 Types, and not just Int32 Types. I have a lot of constant declarations that are UInt32 that I want to put in Enums to ease the use of Intellisence. I...
4
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and...
2
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't...
7
by: moondaddy | last post by:
I want to create a public enum that can be used throughout a project. I created an enum like this in a module: Public Enum ParentType Project = 0 Stage = 1 VIP = 2 Func = 3 Equipment = 4...
2
by: Simon Elliott | last post by:
I have some legacy C++ code which requires some enums to be 1 or 2 bytes in size. I'd ideally like to be able to specify that a few carefully selected enums are a particular size. By default,...
11
by: Marc Gravell | last post by:
This one stumped me while refactoring some code to use generics... Suppose I declare an enum MyEnum {...} Is there a good reason why MyEnum doesn't implement IEquatable<MyEnum> ? Of course,...
3
by: =?Utf-8?B?Sm9uYXRoYW4gU21pdGg=?= | last post by:
I have a class as follows: class GamesConsole { public int iReference; public enum Maker {Nintendo, Sega, Sony, Panasonic} }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.