Last active
December 1, 2015 17:56
-
-
Save HellBrick/375ed91fd6b056fee5d0 to your computer and use it in GitHub Desktop.
Constraint weirdness
This file contains 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
using System; | |
using System.Linq; | |
using System.Linq.Expressions; | |
namespace TheGreatWeirdness | |
{ | |
public interface IKeyHolder | |
{ | |
string Key { get; } | |
} | |
public static class ExpressionGenerator<T> where T : IKeyHolder | |
{ | |
public static Expression<Func<T, bool>> CreateWhereExpression( string[] values ) | |
{ | |
return ( T item ) => values.Contains( item.Key ); | |
} | |
} | |
public class KeyHolder : IKeyHolder | |
{ | |
public string Key => "42"; | |
} | |
class Program | |
{ | |
static void Main( string[] args ) | |
{ | |
Expression expr = ExpressionGenerator<KeyHolder>.CreateWhereExpression( Array.Empty<string>() ); | |
Console.WriteLine( expr.ToString() ); | |
} | |
} | |
} | |
/* The output: | |
item => value(TheGreatWeirdness.ExpressionGenerator`1+<>c__DisplayClass0_0[TheGreatWeirdness.KeyHolder]).values.Contains(Convert(item).Key) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment