Last active
April 16, 2020 20:04
-
-
Save enisn/52be9bf3de77aac1f65a3cdefb985499 to your computer and use it in GitHub Desktop.
Challenge #1 - Solution 2 - StringFilterOptionsAttribute.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [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