Re: Refection question

Richard Blewett <[email protected]> Sat, 3 Mar 2007 09:51:33 -0000
Newsgroups gmane.comp.windows.devel.dotnet.cx
Message-ID <[email protected]>
If you are going to check out the values of custom attributes you will have
to use the reflection API

Regards

Richard Blewett - DevelopMentor

-----Original Message-----
From: Discussion relating to the specifics of the C# and Managed C++
languages [mailto:[email protected]] On Behalf Of Mark Gregory
Sent: 03 March 2007 09:27
To: [email protected]
Subject: [DOTNET-CX] Refection question

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

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

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