Skip to content

Dynamic LINQ execution fails silently or throws parsing error when method returns object #717

@YashTrivedi21

Description

@YashTrivedi21

I am encountering an issue where custom methods registered in ReSettings.CustomTypes or passed via RuleParameter fail to execute or parse if the method's return type is object.

Even when the underlying value returned is a valid primitive (e.g., int or bool) or a known public class, the expression parser seems unable to handle the boxed return type, either failing silently (expression evaluates to false) or throwing a binary operator error.

Reproduction Steps:

  1. Create a simple static method that returns object.
  2. Register the containing class in ReSettings.CustomTypes.
  3. Create a rule that compares the result of this method.

Code Example:

public class SimpleInput
{
    public int Value { get; set; }
    public string Name { get; set; }
}

public class ReturnType
{
    public int val
    {
        get; set;
    }
}

public static class TestFunctions
{
    // This FAILS in expression: "TestFunctions.CheckValue(myObj) == 1"
    public static object CheckValue(SimpleInput input)
    {
        if(input != null && input.Value > 5)
        {
            return new ReturnType { val = 1 };
        }
        return new ReturnType {val = 0 };
    }

    // This WORKS in expression: "TestFunctions.CheckValue(myObj) == 1"
    public static ReturnType CheckValue(SimpleInput input)
    {
        if(input != null && input.Value > 5)
        {
            return new ReturnType { val = 1 };
        }
        return new ReturnType {val = 0 };
    }
}

Rule Definition:

new Rule
{
    RuleName = "TestSpecificClassType",
    RuleExpressionType = RuleExpressionType.LambdaExpression,
    Enabled = true,
    Expression = "TestFunctions.CheckValue(myObj).val == 1",
    ErrorMessage = "Specific Class parameter check failed",
    SuccessEvent = "Specific Class parameter check passed"
}

Observed Behavior:

Scenario A (Implicit Cast):

TestFunctions.CheckValue(myObj) == 1
Result: Exception: Operator Equal is not compatible with operand types 'Object' and 'Int32' (or similar parsing error).

Scenario B (Explicit Cast):

((ReturnType)TestFunctions.CheckValue(myObj)) > 2
Result: Execution fails silently (logs inside the method are never hit), or it evaluates to False unexpectedly.

Expected Behavior:

The engine (via Dynamic LINQ) should be able to unbox the object return type at runtime similar to standard C# or handle the explicit cast correctly within the expression tree to allow comparison.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions