Cinchoo – String.Evaluate(), Part 3

Using custom library routines

You may ask, how to use my own custom routines while evaluating expressions? Here I’ll show you how to do it. All you need to do is to write the custom method as static method of a class, reference it to the current project if it was in build in to separate assembly.

Please see the sample below

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace System


namespace Cinchoo.Core.Test
    public class Utilities
        public static int Sqrt(int x)
            return x * x;

    public class Program
        static void Main(string[] args)
                object x2 = "~~Cinchoo.Core.Test.Utilities.Sqrt(10)~~".Evaluate();
                Console.WriteLine("Output of ~~Cinchoo.Core.Test.Utilities.Sqrt(10)~~: " + x2 + ", ObjectType:" + x2.GetType());
                object x1 = "{{~~Cinchoo.Core.Test.Utilities.Sqrt(10)~~ * 10}}".Evaluate();
                Console.WriteLine("Output of {{~~Cinchoo.Core.Test.Utilities.Sqrt(10)~~ * 10}}: " + x1 + ", ObjectType:" + x1.GetType());
            catch (Exception ex)

In the sample first expression above, the custom method Sqrt is being invoked by surrounding ~~ characters.

In the second expression, I showed how to perform the outcome of Sqrt with other expression by surrounding {{ and }} characters. This is the default delimiters of expressions.

When you run the above code, the output will be

Output of ~~Cinchoo.Core.Test.Utilities.Sqrt(10)~~: 100, ObjectType:System.Int32

Output of {{~~Cinchoo.Core.Test.Utilities.Sqrt(10)~~ * 10}}: 1000, ObjectType:System.Int32

Happy coding!!!

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s