Cinchoo – String.ExpandProperties(), Part 2

Here I’ll show you how you can resolve text contains expressions and properties together.

ExpandProperties will do the expression evaluation along with property replacement in a string, and returns the output as string value. Please see the below samples on how to use it. Expressions are given with {{ and }} delimiters and properties are surrounded by %% delimiters.

How to use

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace System

Sample:

static void Main(string[] args)
{
    Console.WriteLine("Output of {{%%PROCESSOR_COUNT%% * 2}} :" + "{{%%PROCESSOR_COUNT%% * 2}}".ExpandProperties());
    Console.WriteLine("Output of {{%%TICK_COUNT%% / (60 * 60)}} :" + "{{%%TICK_COUNT%% / (60 * 60)}}".ExpandProperties());
    Console.WriteLine(@"Output of {{'%%SYSTEM_DIRECTORY%%' + '\Logs'}} :" + @"{{'%SYSTEM_DIRECTORY%' + '\Logs'}}".ExpandProperties());
}

In the above sample code, all the properties are resolved before expressions evaluated.

When you run the above code, the output will be

Output of {{%%PROCESSOR_COUNT%% * 2}} :4
Output of {{%%TICK_COUNT%% / (60 * 60)}} :384854
Output of {{'%%SYSTEM_DIRECTORY%%' + '\Logs'}} :%SYSTEM_DIRECTORY%\Logs
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