Skip to content

Instantly share code, notes, and snippets.

@enisn
Last active April 16, 2020 20:04
Show Gist options
  • Select an option

  • Save enisn/52be9bf3de77aac1f65a3cdefb985499 to your computer and use it in GitHub Desktop.

Select an option

Save enisn/52be9bf3de77aac1f65a3cdefb985499 to your computer and use it in GitHub Desktop.
Challenge #1 - Solution 2 - StringFilterOptionsAttribute.cs
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class StringFilterOptionsAttribute : FilteringOptionsBaseAttribute
{
public StringFilterOptionsAttribute(StringFilterOption option)
{
this.Option = option;
}
public StringFilterOption Option { get; set; }
public StringComparison StringComparison { get; set; } = StringComparison.InvariantCultureIgnoreCase;
public override Expression BuildExpression(Expression expressionBody, PropertyInfo property, object value)
{
var method = typeof(string).GetMethod(Option.ToString(), types: new[] { typeof(string), typeof(StringComparison) });
var comparison = Expression.Equal(
Expression.Call(
method: method,
instance: Expression.Property(expressionBody, property.Name),
arguments: new[] { Expression.Constant(value), Expression.Constant(StringComparison) }),
Expression.Constant(true)
);
return comparison;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment