Harsha projects

Yet another Tech blog !

Archive for September 2010

Head First OOAD

leave a comment »

I am going through Head First OOAD Book. Its really nice book to learn about OO concepts.  Very nicely explained the concept through Java code.  Here is a code for Chapter 5 Part 2 in C#.

File On Sky Drive

MusicStore.zip On Sky Drive

Interesting part of this exercise was converting enum to string. In java, it is very easy, but in C# we don’t have that feature. But same achieved through “System.Attribute”.  I read this concept from following link:


Anyways, Here is the code:

First you declare your enum with attributes:

    enum InstrumentType

To manage your attribute, new class called StringAttribute is created:

    public class StringAttribute : System.Attribute
        private string stringValue;

        public StringAttribute(string value)
            this.stringValue = value;
        public string StringValue
            get { return stringValue; }


Attributes are retrieved through extension method:

static class ExtensionMethods
        public static string TryGetStringValue(this Enum EnumValue)
            System.Type type = EnumValue.GetType();

            FieldInfo fieldInfo = type.GetField(EnumValue.ToString());

            StringAttribute[] attribs = fieldInfo.GetCustomAttributes(
                typeof(StringAttribute), false) as StringAttribute[];
            return attribs.Length>0 ? attribs[0].StringValue : null;

In the Code, it is accessed like this:

string s = ((InstrumentType)(spec.GetProperty("instrumentType"))).TryGetStringValue();

Hope you will find it useful.

Thanks for visiting the blog.


Written by Harsha

September 10, 2010 at 1:56 AM