<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>C# 3.0 : using extension methods for enum ToString</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/21/483337.aspx</link><description>In my previous blog I was trying to address the issue that when ToString is called on an enum the literal string for the enum constant is returned. Custom attributes can be used to tag localizable description string to the constants so that you can write</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: C# 3.0 : using extension methods for enum ToString</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/21/483337.aspx#483338</link><pubDate>Fri, 21 Oct 2005 08:56:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:483338</guid><dc:creator>mabster</dc:creator><description>Very nice. Can extension methods be properties (I know that sounds weird given that they're called &amp;quot;extension *methods*&amp;quot;)?&lt;br&gt;&lt;br&gt;If so, you could define a &amp;quot;Description&amp;quot; property rather than the (kind of awkward-looking) ToDescription() method.&lt;br&gt;&lt;br&gt;At the very least I think I would call it &amp;quot;ToDescriptionString&amp;quot; (similar to DateTime's ToShortDateString method), or possibly just &amp;quot;GetDescription&amp;quot;.&lt;br&gt;&lt;br&gt;Extension methods are going to change things in a big way, that's for sure.</description></item><item><title>re: C# 3.0 : using extension methods for enum ToString</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/21/483337.aspx#483379</link><pubDate>Fri, 21 Oct 2005 11:40:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:483379</guid><dc:creator>abhinaba</dc:creator><description>According to the latest spec including Extension properties and events are in consideration. Lets hope they finally make it to the product</description></item><item><title>Make all your utility methods 'Extension methods'</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/21/483337.aspx#679165</link><pubDate>Wed, 26 Jul 2006 19:39:18 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:679165</guid><dc:creator>I know the answer (it's 42)</dc:creator><description>As I had previously said I love extension methods&amp;amp;amp;nbsp;and have started using them at multiple places...</description></item><item><title>Why I changed my mind about Extension Methods</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/21/483337.aspx#7429855</link><pubDate>Mon, 04 Feb 2008 10:33:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7429855</guid><dc:creator>James Newton-King</dc:creator><description>&lt;p&gt;My initial impression of extension methods was that they would lead to confusing code. I imagined other&lt;/p&gt;
</description></item><item><title>Cómo describir los elementos de una enumeración usando métodos de extensión y atributos (C# y VB.NET)</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/21/483337.aspx#8463470</link><pubDate>Tue, 06 May 2008 20:14:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8463470</guid><dc:creator>Variable not found en Geeks.ms</dc:creator><description>&lt;p&gt;Me he encontrado en el blog de Fresh Logic Studios con un post donde describen una t&amp;#233;cnica interesante&lt;/p&gt;
</description></item><item><title>re: C# 3.0 : using extension methods for enum ToString</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/21/483337.aspx#8639104</link><pubDate>Sun, 22 Jun 2008 18:43:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8639104</guid><dc:creator>Sergey</dc:creator><description>&lt;p&gt;I've created another&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;public static string ConvertToString(this Enum value)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (value == null)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;throw new ArgumentNullException(&amp;quot;value&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Type type = value.GetType();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;FieldInfo fieldInfo = type.GetField(Enum.GetName(type, value));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;var descriptionAttribute =&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;(DescriptionAttribute)Attribute.GetCustomAttribute(&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; fieldInfo, typeof(DescriptionAttribute));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (descriptionAttribute != null)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return descriptionAttribute.Description;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return value.ToString();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
</description></item><item><title>C# : Enum and overriding ToString on it</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/21/483337.aspx#9440958</link><pubDate>Mon, 23 Feb 2009 07:43:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9440958</guid><dc:creator>I know the answer (it's 42)</dc:creator><description>&lt;p&gt;I saw two posts on Enums today on Eric Lipperts and Chris Rathjen's blog. Enums are significantly different&lt;/p&gt;
</description></item><item><title>re: C# 3.0 : using extension methods for enum ToString</title><link>http://blogs.msdn.com/abhinaba/archive/2005/10/21/483337.aspx#9762581</link><pubDate>Tue, 16 Jun 2009 19:06:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9762581</guid><dc:creator>Michael</dc:creator><description>&lt;p&gt;Here is a better implementation... takes care of caching so reflection does not take such a huge hit.&lt;/p&gt;
&lt;p&gt;	public static class EnumExtensionMethods&lt;/p&gt;
&lt;p&gt;	{&lt;/p&gt;
&lt;p&gt;		#region [ Fields ]&lt;/p&gt;
&lt;p&gt;		private static Dictionary&amp;lt;string, string&amp;gt; _enumTypeDescriptionCache = new Dictionary&amp;lt;string, string&amp;gt;();&lt;/p&gt;
&lt;p&gt;		private static object _enumTypeDescriptionCacheLock = new object();&lt;/p&gt;
&lt;p&gt;		#endregion&lt;/p&gt;
&lt;p&gt;		#region [ Methods ]&lt;/p&gt;
&lt;p&gt;		/// &amp;lt;summary&amp;gt;&lt;/p&gt;
&lt;p&gt;		/// &lt;/p&gt;
&lt;p&gt;		/// &amp;lt;/summary&amp;gt;&lt;/p&gt;
&lt;p&gt;		/// &amp;lt;param name=&amp;quot;en&amp;quot;&amp;gt;&amp;lt;/param&amp;gt;&lt;/p&gt;
&lt;p&gt;		/// &amp;lt;returns&amp;gt;&amp;lt;/returns&amp;gt;&lt;/p&gt;
&lt;p&gt;		public static string ToDescription(this Enum en) &amp;nbsp; &amp;nbsp;&lt;/p&gt;
&lt;p&gt;		{&lt;/p&gt;
&lt;p&gt;			string cacheKey;&lt;/p&gt;
&lt;p&gt;			string description;&lt;/p&gt;
&lt;p&gt;			cacheKey = string.Format(&amp;quot;{0}:{1}&amp;quot;, en.GetType().FullName, en.ToString());&lt;/p&gt;
&lt;p&gt;			description = string.Empty;&lt;/p&gt;
&lt;p&gt;			if (_enumTypeDescriptionCache.ContainsKey(cacheKey) == false)&lt;/p&gt;
&lt;p&gt;			{&lt;/p&gt;
&lt;p&gt;				lock (_enumTypeDescriptionCacheLock)&lt;/p&gt;
&lt;p&gt;				{&lt;/p&gt;
&lt;p&gt;					if (_enumTypeDescriptionCache.ContainsKey(cacheKey) == false)&lt;/p&gt;
&lt;p&gt;					{&lt;/p&gt;
&lt;p&gt;						MemberInfo[] memberInfo;&lt;/p&gt;
&lt;p&gt;						memberInfo = en.GetType().GetMember(en.ToString());&lt;/p&gt;
&lt;p&gt;						if (memberInfo != null &amp;amp;&amp;amp; memberInfo.Length &amp;gt; 0)&lt;/p&gt;
&lt;p&gt;						{&lt;/p&gt;
&lt;p&gt;							object[] attributes;&lt;/p&gt;
&lt;p&gt;							attributes = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);&lt;/p&gt;
&lt;p&gt;							if (null != attributes &amp;amp;&amp;amp; attributes.Length &amp;gt; 0)&lt;/p&gt;
&lt;p&gt;							{&lt;/p&gt;
&lt;p&gt;								description =((DescriptionAttribute)attributes[0]).Description;&lt;/p&gt;
&lt;p&gt;							}&lt;/p&gt;
&lt;p&gt;						}&lt;/p&gt;
&lt;p&gt;					}&lt;/p&gt;
&lt;p&gt;					else&lt;/p&gt;
&lt;p&gt;					{&lt;/p&gt;
&lt;p&gt;						description = _enumTypeDescriptionCache[cacheKey];&lt;/p&gt;
&lt;p&gt;					}&lt;/p&gt;
&lt;p&gt;				}&lt;/p&gt;
&lt;p&gt;			}&lt;/p&gt;
&lt;p&gt;			else&lt;/p&gt;
&lt;p&gt;			{&lt;/p&gt;
&lt;p&gt;				description = _enumTypeDescriptionCache[cacheKey];&lt;/p&gt;
&lt;p&gt;			}&lt;/p&gt;
&lt;p&gt;			return description;&lt;/p&gt;
&lt;p&gt;		}&lt;/p&gt;
&lt;p&gt;		#endregion&lt;/p&gt;
&lt;p&gt;	}&lt;/p&gt;
</description></item></channel></rss>