Click here to Skip to main content
15,891,248 members
Articles / Web Development / ASP.NET

Zeta Enterprise Library

Rate me:
Please Sign up or sign in to vote.
4.97/5 (14 votes)
16 Jan 2010CPOL3 min read 50.3K   2.3K   48  
A small set of general-purpose classes for using in .NET applications (2.0 or higher)
namespace Zeta.EnterpriseLibrary.Tools
{
	using System;
	using System.ComponentModel;
	using System.Globalization;
	using System.Reflection;

	/// <summary>
	/// EnumConverter supporting System.ComponentModel.DescriptionAttribute
	/// </summary>
	public class EnumDescriptionConverter : EnumConverter
	{
		private readonly Type _myVal;

		/// <summary>
		/// Initializes a new instance of the <see cref="EnumDescriptionConverter"/> class.
		/// </summary>
		/// <param name="type">A <see cref="T:System.Type"/> that represents the type of enumeration to associate with this enumeration converter.</param>
		public EnumDescriptionConverter( Type type )
			: base( type )
		{
			_myVal = type;
		}

		/// <summary>
		/// Gets Enum Value's Description Attribute
		/// </summary>
		/// <param name="value">The value you want the description attribute for</param>
		/// <returns>The description, if any, else it's .ToString()</returns>
		public static string GetEnumDescription( Enum value )
		{
			var fi = value.GetType().GetField( value.ToString() );
			var attributes =
				(DescriptionAttribute[])fi.GetCustomAttributes(
											 typeof( DescriptionAttribute ), false );
			return (attributes.Length > 0) ? attributes[0].Description : value.ToString();
		}

		/// <summary>
		/// Gets the description for a certain named value in an Enumeration
		/// </summary>
		/// <param name="value">The type of the Enumeration</param>
		/// <param name="name">The name of the Enumeration value</param>
		/// <returns>The description, if any, else the passed name</returns>
		public static string GetEnumDescription( Type value, string name )
		{
			var fi = value.GetField( name );
			var attributes =
				(DescriptionAttribute[])fi.GetCustomAttributes(
											 typeof( DescriptionAttribute ), false );
			return (attributes.Length > 0) ? attributes[0].Description : name;
		}

		/// <summary>
		/// Gets the value of an Enum, based on it's Description Attribute or named value
		/// </summary>
		/// <param name="value">The Enum type</param>
		/// <param name="description">The description or name of the element</param>
		/// <returns>The value, or the passed in description, if it was not found</returns>
		public static object GetEnumValue( Type value, string description )
		{
			FieldInfo[] fis = value.GetFields();
			foreach ( FieldInfo fi in fis )
			{
				var attributes =
					(DescriptionAttribute[])fi.GetCustomAttributes(
												 typeof( DescriptionAttribute ), false );
				if ( attributes.Length > 0 )
				{
					if ( attributes[0].Description == description )
					{
						return fi.GetValue( fi.Name );
					}
				}
				if ( fi.Name == description )
				{
					return fi.GetValue( fi.Name );
				}
			}
			return description;
		}


		/// <summary>
		/// Determines whether this instance [can convert from] the specified context.
		/// </summary>
		/// <param name="context">The context.</param>
		/// <param name="sourceType">Type of the source.</param>
		/// <returns>
		/// 	<c>true</c> if this instance [can convert from] the specified context; otherwise, <c>false</c>.
		/// </returns>
		public override bool CanConvertFrom( ITypeDescriptorContext context, Type sourceType )
		{
			return sourceType == typeof( Enum ) || base.CanConvertFrom( context, sourceType );
		}

		/// <summary>
		/// Determines whether this instance [can convert to] the specified context.
		/// </summary>
		/// <param name="context">The context.</param>
		/// <param name="destinationType">Type of the destination.</param>
		/// <returns>
		/// 	<c>true</c> if this instance [can convert to] the specified context; otherwise, <c>false</c>.
		/// </returns>
		public override bool CanConvertTo( ITypeDescriptorContext context, Type destinationType )
		{
			return destinationType == typeof( string ) || base.CanConvertTo( context, destinationType );
		}

		/// <summary>
		/// Converts to.
		/// </summary>
		/// <param name="context">The context.</param>
		/// <param name="culture">The culture.</param>
		/// <param name="value">The value.</param>
		/// <param name="destinationType">Type of the destination.</param>
		/// <returns></returns>
		public override object ConvertTo( ITypeDescriptorContext context, CultureInfo culture, object value,
										 Type destinationType )
		{
			if ( value is Enum && destinationType == typeof( string ) )
			{
				return GetEnumDescription( (Enum)value );
			}
			if ( value is string && destinationType == typeof( string ) )
			{
				return GetEnumDescription( _myVal, (string)value );
			}
			return base.ConvertTo( context, culture, value, destinationType );
		}

		/// <summary>
		/// Converts from.
		/// </summary>
		/// <param name="context">The context.</param>
		/// <param name="culture">The culture.</param>
		/// <param name="value">The value.</param>
		/// <returns></returns>
		public override object ConvertFrom( 
			ITypeDescriptorContext context, 
			CultureInfo culture, 
			object value )
		{
			if ( value is string )
			{
				return GetEnumValue( _myVal, (string)value );
			}
			if ( value is Enum )
			{
				return GetEnumValue( _myVal, value.ToString() );
			}
			return base.ConvertFrom( context, culture, value );
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Zeta Software GmbH
Germany Germany
Uwe does programming since 1989 with experiences in Assembler, C++, MFC and lots of web- and database stuff and now uses ASP.NET and C# extensively, too. He has also teached programming to students at the local university.

➡️ Give me a tip 🙂

In his free time, he does climbing, running and mountain biking. In 2012 he became a father of a cute boy and in 2014 of an awesome girl.

Some cool, free software from us:

Windows 10 Ereignisanzeige  
German Developer Community  
Free Test Management Software - Intuitive, competitive, Test Plans.  
Homepage erstellen - Intuitive, very easy to use.  
Offline-Homepage-Baukasten

Comments and Discussions