0

I'm using the Flee library to evaluate expressions that the users enters. I'm using the fact that expressions don't compile to warn the user of invalid input.

If I use an expression like var=="something" it will not compile if var is an int, because this is not valid code.

But if var is an object of a custom class I made it will compile (since the object has a ToString method I guess). How can I prevent the expression with the custom class from compiling if the variable that is contained in there can't be compared to string?

Update: here is an example piece of code that shows the problem. filter1 will throw an error when trying to compile with the message:

Ciloci.Flee.ExpressionCompileException: CompareElement: Operation 'Equal' is not defined for types 'Int32' and 'String'

But the second filter that using the custom class as variable type instead of the integer will not give an exception. But I want that custom class to behave like the int and not have an Equal operation defined. How to do that?

using System;
using Ciloci.Flee;

namespace FleeTest
{
    class VariableClass
    {
        // TODO
    }

    class Program
    {
        public static void Main(string[] args)
        {
            ExpressionContext context = new ExpressionContext();
            context.Options.ResultType = typeof(bool);

            VariableCollection var = context.Variables;
            var.ResolveVariableType += var_ResolveVariableType;

            string filter1 = "varInt=\"something\"";
            string filter2 = "varClass=\"something\"";

            try
            {
                IDynamicExpression e = context.CompileDynamic(filter1); 
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }

            try
            {
                IDynamicExpression e = context.CompileDynamic(filter2); 
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }

        }

        static void var_ResolveVariableType(object sender, ResolveVariableTypeEventArgs e)
        {
            if(e.VariableName == "varInt")
            {
                e.VariableType = typeof(int);
            }
            else if(e.VariableName == "varClass")
            {
                e.VariableType = typeof(VariableClass);
            }
        }
    }
}

1 Answers1

0

maybe try to convert your var like this:

  if(Convert.ToString(var) == "something")
  {
  //code
  }

I hope it's your probleme and it's help. Bye :)