Example for OptionAllowNonPublicMembersAccess with ExpressionEvaluator

CSharp · Console

/Program.cs

/**********************************************************************************************
* This fiddle is a demo of the library : CodingSeb.ExpressionEvaluator
* See : https://github.com/codingseb/ExpressionEvaluator
* For Others examples : https://dotnetfiddle.net/Packages/41132/CodingSeb_ExpressionEvaluator
**********************************************************************************************/

using System;
using System.Collections.Generic;
using CodingSeb.ExpressionEvaluator;
					
public class TestClassForPrivateFields
{
	private int myInt = 5;
	
	private string MyString 
	{
		get
		{
			return "Hey I am private";	
		}
	}
	
	private string PrivateMethod()
	{
		return "Hello from a private place";	
	}
}

public class Program
{
	public static void Main()
	{
		ExpressionEvaluator evaluator = new ExpressionEvaluator(new Dictionary<string, object>()
		{
			{"myVar", new TestClassForPrivateFields()}
		});
		
		List<string> expressions = new List<string>()
		{
			"myVar.myInt",
			"myVar.MyString",
			"myVar.PrivateMethod()",
		};
		
		Console.WriteLine("Test Expressions without OptionAllowNonPublicMembersAccess.");
		Console.WriteLine(string.Empty);
		
		expressions.ForEach(expression => 
		{
			try
			{
				Console.WriteLine(expression);
				Console.WriteLine(evaluator.Evaluate(expression));
			}
			catch(Exception exception)
			{
				Console.WriteLine(exception);
			}
			finally
			{
				Console.WriteLine(string.Empty);	
			}
		});
		
		evaluator.OptionAllowNonPublicMembersAccess = true;
		
		Console.WriteLine("Test Expressions with OptionAllowNonPublicMembersAccess = true");
		Console.WriteLine(string.Empty);
		
		expressions.ForEach(expression => 
		{
			try
			{
				Console.WriteLine(expression);
				Console.WriteLine(evaluator.Evaluate(expression));
			}
			catch(Exception exception)
			{
				Console.WriteLine(exception);
			}
			finally
			{
				Console.WriteLine(string.Empty);	
			}
		});
	}
}
;