Download CustomTypeConverter1.zip version 1.0.0.0, last updated 28/07/2013 (12.74 KB)

Download
  • md5: b750911ae2f67565a562ace5615f3f06
using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;

/*
  Custom Type Converter Sample 1
  http://cyotek.com/blog/creating-a-custom-typeconverter-part-1
*/

namespace CustomTypeConverter1
{
  /// <summary>
  /// Converts lengths from one data type to another. Access this class through the <see cref="TypeDescriptor"/>.
  /// </summary>
  internal class LengthConverter : TypeConverter
  {
    #region Overridden Members

    /// <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" /> that provides a format context.</param>
    /// <param name="sourceType">A <see cref="T:System.Type" /> 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 the given object to the type of this converter, using the specified context and culture information.
    /// </summary>
    /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context.</param>
    /// <param name="culture">The <see cref="T:System.Globalization.CultureInfo" /> to use as the current culture.</param>
    /// <param name="value">The <see cref="T:System.Object" /> to convert.</param>
    /// <returns>An <see cref="T:System.Object" /> that represents the converted value.</returns>
    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
      string stringValue;
      object result;

      result = null;
      stringValue = value as string;

      if (!string.IsNullOrEmpty(stringValue))
      {
        int nonDigitIndex;

        nonDigitIndex = stringValue.IndexOf(stringValue.FirstOrDefault(char.IsLetter));

        if (nonDigitIndex > 0)
        {
          result = new Length
          {
            Value = Convert.ToSingle(stringValue.Substring(0, nonDigitIndex)),
            Unit = (Unit)Enum.Parse(typeof(Unit), stringValue.Substring(nonDigitIndex), true)
          };
        }
      }

      return result ?? base.ConvertFrom(context, culture, value);
    }

    /// <summary>
    /// Converts the given value object to the specified type, using the specified context and culture information.
    /// </summary>
    /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext" /> that provides a format context.</param>
    /// <param name="culture">A <see cref="T:System.Globalization.CultureInfo" />. If null is passed, the current culture is assumed.</param>
    /// <param name="value">The <see cref="T:System.Object" /> to convert.</param>
    /// <param name="destinationType">The <see cref="T:System.Type" /> to convert the <paramref name="value" /> parameter to.</param>
    /// <returns>An <see cref="T:System.Object" /> that represents the converted value.</returns>
    /// <exception cref="System.ArgumentNullException">destinationType</exception>
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
      Length length;
      object result;

      result = null;
      length = value as Length;

      if (length != null && destinationType == typeof(string))
        result = length.ToString();

      return result ?? base.ConvertTo(context, culture, value, destinationType);
    }

    #endregion
  }
}

Donate

Donate