Cinchoo – ChoObject.ToString(), Part 10

Defining and using TypeConverter

Sometime you may want to customize the object member string representation. One way of achieving it by using TypeConverters. Either you can create a new TypeConverter or use the existing .NET type converters.

Defining TypeConverter

 Please visit MSDN on How to: Implement a Type Converter. Below is the sample converter
public class ChoArrayToStringConverter : TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(string))
            return true;
        else
            return base.CanConvertTo(context, destinationType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
    {
        if (value == null)
            return null;

        StringBuilder msg = new StringBuilder();
        if (value.GetType().IsArray)
        {
            foreach (object item in (Array)value)
            {
                if (msg.Length == 0)
                    msg.Append(ChoString.ToString(item, String.Empty, String.Empty));
                else
                    msg.AppendFormat(", {0}", ChoString.ToString(item, String.Empty, String.Empty));
            }
            return msg.ToString();
        }
        return base.ConvertFrom(context, culture, value);
    }
}

Please visit .NET TypeConverters for the list of Type Converters that .NET provides. Those can be used in here in converting members to string represent.

Using TypeConverter

[ChoTypeFormatter("'{this.Name}' Security State")]
public class Portfolio : ChoObject
{
    public string Name;
    public string Description;
    [ChoMemberFormatter(TypeConverter = typeof(ChoArrayToStringConverter))]
    public string[] Securities;
    [ChoMemberFormatter(TypeConverter = typeof(BooleanConverter))]
    public bool Live;

    public Portfolio(string name, string description, string[] securities)
    {
        Name = name;
        Description = description;
        Securities = securities;
    }
}

static void Main(string[] args)
{
    try
    {
        Portfolio portfolio = new Portfolio("High-Growth", "High Growth Portfolio", new string[] { "AAPL", "GOOG", "IBM" });
        Console.WriteLine(portfolio.ToString());
    }
    finally
    {
        ChoAppDomain.Exit();
    }
}

The output will be

-- 'High-Growth' Security State --
        Name: High-Growth
        Description: High Growth Portfolio
        Securities: AAPL, GOOG, IBM
        Live: False

Press any key to continue . . .

Happy coding!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s