Given the following classes
public class ClassA
{
public string StringProperty { get; set; }
public List<ClassB> List { get; set; }
}
public class ClassB
{
public int IntProperty { get; set; }
}
I Would like to dynamically create an expression like the following
x => x.StringProperty == "X" && x.List.Any( y => y.IntProperty > 1 )
No problem for the first part (x.StringProperty == "X"
). For the second part I have created a member expression corresponding to x.List
and now need to
- Create the inner lambda. To do this I need to know the type of
y
which is actually the same inner type ofx.List
- Call the Any method on the x.List expression
Any hint on the first point? How do I get the type T of an IEnumerable<T>
?
EDIT
I have tried with the following code but it returns null unfortunately
//This expression will be x.List of my original sample
MemberExpression expr = GetMemberExpression( property, pe );
Type innerType = expr.GetType()
.GetInterfaces()
.Where( t => t.IsGenericType == true && t.GetGenericTypeDefinition() == typeof( IEnumerable<> ) )
.Select( t => t.GetGenericArguments()[0] )
.SingleOrDefault();