mardok
mardok

Reputation: 2415

Spring expressions and nullable values

I use Spring.Core dll with ExpressionEvaluator .

My simple code:

public void ComputedFields()
{
  var finance = new Finance();
  ExpressionEvaluator
    .SetValue(
      finance, 
      "FieldToBeSet", 
      ExpressionEvaluator
        .GetValue(
          finance, 
          "SomeNullableFinanceField.GetValueOrDefault(1)"));

  Assert.AreEqual(finance.FieldToBeSet, 1);
} 

raises the following exception:

Method GetValueOrDefault with the specified number and types of arguments does not exist.

Finance is a simple poco the SomeNullableFinanceField field is of type decimal?

Upvotes: 1

Views: 291

Answers (1)

Kirill Muratov
Kirill Muratov

Reputation: 368

Spring.net ExpressionEvaluator doesn't know about .net Nullable types, so it doesn't try to find and invoke GetValueOrDefault method because of SomeNullableFinanceField is null.

If SomeNullableFinanceField is not null, it tries to invoke GetValueOrDefault on SomeNullableFinanceField value (decimal).

You should modify you expression:

ExpressionEvaluator
    .GetValue(
      finance, 
      "SomeNullableFinanceField != null ? SomeNullableFinanceField : 1");

or

ExpressionEvaluator
    .GetValue(
      finance, 
      "{SomeNullableFinanceField, 1}.nonNull()[0]");

Upvotes: 1

Related Questions