Skip to content

Instantly share code, notes, and snippets.

@HellBrick
Last active December 1, 2015 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HellBrick/375ed91fd6b056fee5d0 to your computer and use it in GitHub Desktop.
Save HellBrick/375ed91fd6b056fee5d0 to your computer and use it in GitHub Desktop.
Constraint weirdness
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