Paulo Morgado

.NET Development & Architecture

This Blog

Syndication

Search

Tags

News

Unit Test Today! Get Typemock Isolator!

Projects

Books

 

Visitors

Visitor Locations

Community

Email Notifications

Archives

Profile

Disclaimer

The opinions and viewpoints expressed in this site are mine and do not necessarily reflect those of Microsoft, my employer or any community that I belong to. Any code or opinions are offered as is. Products or services mentioned are purchased by me, made available to me by my employer or the manufacturer/vendor which doesn't influence my opinion in any way.

TypeTypeConverter - The Type TypeConverter

I've been searching high and low for a TypeConverter for Types and only found private or internal implementations.

It wasn't a hard task, but I think that the .NET Framework should provide one out of the box.

Here is the one I wrote:

/// <summary>
/// Provides a type converter to convert <see cref="T:System.Type"/> objects to and from various other representations.
/// </summary>
public class TypeTypeConverter : System.ComponentModel.TypeConverter
{
    /// <summary>
    /// Returns whether this converter can convert an object of the given type to the type of this converter, using the specified context.
    /// </summary>
    /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context.</param>
    /// <param name="sourceType">A <see cref="T:System.Type"></see> that represents the type you want to convert from.</param>
    /// <returns>
    /// true if this converter can perform the conversion; otherwise, false.
    /// </returns>
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
    }

    /// <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)
        {
            Type type = Type.GetType((string)value);
            if (type == null)
            {
                throw new ArgumentException("Type not found.", "value");
            }
            return type;
        }
        return base.ConvertFrom(context, culture, value);
    }

    /// <summary>
    /// Returns whether this converter can convert the object to the specified type, using the specified context.
    /// </summary>
    /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that provides a format context.</param>
    /// <param name="destinationType">A <see cref="T:System.Type"></see> that represents the type you want to convert to.</param>
    /// <returns>
    /// true if this converter can perform the conversion; otherwise, false.
    /// </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 (destinationType != typeof(string))
        {
            return base.ConvertTo(context, culture, value, destinationType);
        }
        return ((Type)value).AssemblyQualifiedName;
    }
}

Published Mon, Jul 9 2007 0:31 by Paulo Morgado

Comments

# Links for 7/14/07 [my NetNewsWire tabs] | What&#8217;s the frequency, Kenneth?@ Saturday, July 14, 2007 5:08 PM

Pingback from  Links for 7/14/07 [my NetNewsWire tabs] | What's the frequency, Kenneth?

Links for 7/14/07 [my NetNewsWire tabs] | What’s the frequency, Kenneth?

# TypeTypeConverter vs. TypeNameConverter@ Sunday, November 25, 2007 4:38 PM

Some time ago I complained about the fact that a TypeConverter for Type s was missing from the .NET Framework

Paulo Morgado

# re: TypeTypeConverter - The Type TypeConverter@ Friday, February 05, 2010 5:12 AM

Thank you very much for that nice piece of code, comes in very handy.

Truly, this should have been provided by the Framework itself.

BeowulfOF

# re: TypeTypeConverter - The Type TypeConverter@ Friday, February 05, 2010 7:21 AM

In fact, I later found out that there's a System.Configuration.TypeNameConverter.

I hadn't found it yet because of the lousy naming.

Thanks,

Paulo

Paulo Morgado

Leave a Comment

(required) 
(required) 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: