Cinchoo – String.ExpandProperties(), Extension Method

In this section, I’ll talk about ExpandProperties extension method. This method used to find and replace any system / user defined properties found in a string. Properties are string begins and ends in %% separatorPlease see the below samples on how to use it. This routine returns string value.

Sample Properties are

%%APPLICATION_NAME%%
%%TODAY%%

How to use

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace System

Sample:

static void Main(string[] args)
{
    //Simple property expansion
    Console.WriteLine("Output of '%%APPLICATION_NAME%%' property:" + " %%APPLICATION_NAME%%".ExpandProperties());
    Console.WriteLine("Output of '%%TODAY%%' property:" + " %%TODAY%%".ExpandProperties());
    Console.WriteLine("Output of '%%PROCESSOR_COUNT%%' property:" + " %%PROCESSOR_COUNT%%".ExpandProperties());

    Console.WriteLine();

    //String containing properties
    Console.WriteLine("This machine has %%PROCESSOR_COUNT%% processors.".ExpandProperties());
}

When you run the above code, the output will be

Output of '%%APPLICATION_NAME%%' property: Cinchoo.Core.Test.exe
Output of '%%TODAY%%' property: 2/16/2012
Output of '%%PROCESSOR_COUNT%%' property: 2

This machine has 2 processors.

Press any key to continue . . .

Formatting Property Value

Some cases, you may want to format the property value. It can be done by passing format specification along with property name. Property Name and format specification are separated by ^ character. All .NET specific format specifiers are valid.

PS: Please refer Formatting Types from MSDN for more information about format specifiers used in .NET

%%PROPERTY_NAME^FORMAT_STRING%%

Sample:

static void Main(string[] args)
{
    //Formatted property expansion
    Console.WriteLine("Output of %%TODAY^yyyyMMdd%% property:" + " %%TODAY^yyyyMMdd%%".ExpandProperties());
    Console.WriteLine("Output of %%PROCESSOR_COUNT^#,#.00#;(#,#.00#)%% property:" + " %%PROCESSOR_COUNT^#,#.00#;(#,#.00#)%%".ExpandProperties());
    Console.WriteLine("Output of %%RANDOM_NO^C%% property:" + " %%RANDOM_NO^C%%".ExpandProperties());
}

When you run the above code, the output will be

Output of %%TODAY^yyyyMMdd%% property: 20120216
Output of %%PROCESSOR_COUNT^#,#.00#;(#,#.00#)%% property: 2.00
Output of %%RANDOM_NO^C%% property: ($92,640,205.00)

Press any key to continue . . .

Handling of Unknown Property

Cinchoo framework gracefully skips any unknown properties specified in the input string. Look below the sample

Sample:

static void Main(string[] args)
{
    Console.WriteLine("Output of %%UNKNOWN_PROPERTY^C%% property:" + " %UNKNOWN_PROPERTY^C%".ExpandProperties());
}

When you run the above code, the output will be

Output of %%UNKNOWN_PROPERTY^C%% property: %UNKNOWN_PROPERTY^C%

Press any key to continue . . .

How to resolve Unknown Property

Cinchoo framework provides a event handler to resolve any unknown properties failed to resolve. The event is exposed in ChoApplication.PropertyResolve event. Look at the sample below on how to handle this situation

Sample:

static void Main(string[] args)
{
    //Hook up the event handler
    ChoApplication.PropertyResolve += new EventHandler<Property.ChoUnknownProperyEventArgs>(PropertyResolve);

    Console.WriteLine("Output of %%UNKNOWN_PROPERTY^C%% property:" + " %%UNKNOWN_PROPERTY^C%%".ExpandProperties());
}

//Event handler
private static void PropertyResolve(object sender, Property.ChoUnknownProperyEventArgs e)
{
    if (e.PropertyName == "UNKNOWN_PROPERTY")
    {
        e.PropertyValue = "RESOLVED_VALUE";
        e.Resolved = true;
    }
}

When you run the above code, the output will be

Output of %%UNKNOWN_PROPERTY^C%% property: RESOLVED_VALUE

Press any key to continue . . .

Try it out! 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