Refection question

Mark Gregory <[email protected]> Sat, 3 Mar 2007 20:27:27 +1100
Newsgroups gmane.comp.windows.devel.dotnet.cx
Message-ID <[email protected]>
I have a class that uses reflection in the ToString function. Is there
another way to do this without reflection in this case? I'm trying to
simplify my code and I seem to remember another way to do this, but
can't seem to remember how.

public class CEnumDescriptionConverter : EnumConverter
        {
                public CEnumDescriptionConverter(Type type) : base(type)
                {
                }

                public static string ToString(object value)
                {
                        // Get the type from the object.
                        Type objType = value.GetType();

                        // Get the member on the type that corresponds
to the value passed in.
                        System.Reflection.FieldInfo objFieldInfo =
objType.GetField(Enum.GetName(objType, value));

                        // Now get the attribute on the field.
                        DescriptionAttribute objAttribute =
(DescriptionAttribute)
	
(objFieldInfo.GetCustomAttributes(typeof(DescriptionAttribute),
false)[0]);

                        return objAttribute.Description;
                }
}

Regards, mark

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com