Skip to content

Commit

Permalink
Support for dynamic types like expando object #36
Browse files Browse the repository at this point in the history
Added validation if object inherits (or is) an IDictionary<string, object>, and return the value if the key exists
  • Loading branch information
PeterHagen committed Jul 11, 2021
1 parent b2b3a1a commit 09ffd3b
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/NReco.LambdaParser/Linq/LambdaParameterWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ internal sealed class LambdaParameterWrapper : IComparable, ILambdaValue {
if (obj is LambdaParameterWrapper)
obj = ((LambdaParameterWrapper)obj).Value;

// An epandoObject has a IDictionary underneath
object objectValue;
if (obj is IDictionary<string, object> dictionary && dictionary.TryGetValue(propertyName, out objectValue))
{
return new LambdaParameterWrapper(objectValue, Cmp);
}

#if NET40
var prop = obj.GetType().GetProperty(propertyName);
#else
Expand All @@ -140,6 +147,7 @@ internal sealed class LambdaParameterWrapper : IComparable, ILambdaValue {
var propVal = prop.GetValue(obj, null);
return new LambdaParameterWrapper(propVal, Cmp);
}

#if NET40
var fld = obj.GetType().GetField(propertyName);
#else
Expand All @@ -149,6 +157,7 @@ internal sealed class LambdaParameterWrapper : IComparable, ILambdaValue {
var fldVal = fld.GetValue(obj);
return new LambdaParameterWrapper(fldVal, Cmp);
}

throw new MissingMemberException(obj.GetType().ToString()+"."+propertyName);
}

Expand Down

0 comments on commit 09ffd3b

Please sign in to comment.