Cinchoo – ChoObject.ToString(), Part 6

Handling of Dictionary members

Cinchoo framework seamlessly support the dictionary members while constructing string represent of a type.

For a type below having dictionary member

[ChoTypeFormatter("'{this.Name}' Security State")]
public class Portfolio : ChoObject
{
    public string Name;
    public string Description;
    public Dictionary<string, Security> Securities;

    public Portfolio(string name, string description, Security[] securities)
    {
        Name = name;
        Description = description;
        Securities = new Dictionary<string, Security>();

        foreach (Security sec in securities)
            Securities.Add(sec.Symbol, sec);
    }
}

static void Main(string[] args)
{
    try
    {
        Security sec1 = new Security("AAPL", "Apple Inc", 458.25);
        Security sec2 = new Security("GOOG", "Google Inc", 658.25);
        Security sec3 = new Security("IBM", "International Business Machines Inc", 198.50);

        Portfolio portfolio = new Portfolio("High-Growth", "High Growth Portfolio", new Security[] { sec1, sec2, sec3 });
        Console.WriteLine(portfolio.ToString());
    }
    finally
    {
        ChoAppDomain.Exit();
    }
}

The output will be

-- 'High-Growth' Security State --
	Name: High-Growth
	Description: High Growth Portfolio
	Securities [Length: 3]:
		-- System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[Cinchoo.Core.Test.Security, Cinchoo.Core.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] Dump --
			Key: AAPL
			Value:
				-- 'AAPL' Security State --
					Symbol: AAPL
					CompanyName: Apple Inc
					Price: 458.25
					x [Length: 3]:
						AAPL1
						APPL2
						APPL3

		-- System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[Cinchoo.Core.Test.Security, Cinchoo.Core.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] Dump --
			Key: GOOG
			Value:
				-- 'GOOG' Security State --
					Symbol: GOOG
					CompanyName: Google Inc
					Price: 658.25
					x [Length: 3]:
						AAPL1
						APPL2
						APPL3

		-- System.Collections.Generic.KeyValuePair`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[Cinchoo.Core.Test.Security, Cinchoo.Core.Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] Dump --
			Key: IBM
			Value:
				-- 'IBM' Security State --
					Symbol: IBM
					CompanyName: International Business Machines Inc
					Price: 198.5
					x [Length: 3]:
						AAPL1
						APPL2
						APPL3

Press any key to continue . . .

If you want to simplify the title of each security item to ‘Security’, you can do so by declaring ChoMemberItemFormatter attribute (a attribute used to format each item in a collection) as below

[ChoTypeFormatter("'{this.Name}' Security State")]
public class Portfolio : ChoObject
{
    public string Name;
    public string Description;
    [ChoMemberItemFormatter("Security")]
    public Dictionary<string, Security> Securities;

    public Portfolio(string name, string description, Security[] securities)
    {
        Name = name;
        Description = description;
        Securities = new Dictionary<string, Security>();

        foreach (Security sec in securities)
            Securities.Add(sec.Symbol, sec);
    }
}

Output will be

-- 'High-Growth' Security State --
	Name: High-Growth
	Description: High Growth Portfolio
	Securities [Length: 3]:
		-- Security --
			Key: AAPL
			Value:
				-- 'AAPL' Security State --
					Symbol: AAPL
					CompanyName: Apple Inc
					Price: 458.25
					x [Length: 3]:
						AAPL1
						APPL2
						APPL3

		-- Security --
			Key: GOOG
			Value:
				-- 'GOOG' Security State --
					Symbol: GOOG
					CompanyName: Google Inc
					Price: 658.25
					x [Length: 3]:
						AAPL1
						APPL2
						APPL3

		-- Security --
			Key: IBM
			Value:
				-- 'IBM' Security State --
					Symbol: IBM
					CompanyName: International Business Machines Inc
					Price: 198.5
					x [Length: 3]:
						AAPL1
						APPL2
						APPL3

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