Create an Expression using reflection

The code to create the expression dynamically would be like this: ParameterExpression parameter = Expression.Parameter(typeof (GoalsModelUnitOfWork), “i”); MemberExpression property = Expression.Property(parameter, “AcademicCycles”); var queryableType = typeof (IQueryable<>).MakeGenericType(typeof (AcademicCycle)); var delegateType = typeof (Func<,>).MakeGenericType(typeof (GoalsModelUnitOfWork), queryableType); var yourExpression = Expression.Lambda(delegateType, property, parameter); The result will have the desired type, but the problem is that the return … Read more

Regular Expression in Bash Script

=~ succeeds if the string on the left contains a match for the regex on the right. If you want to know if the string matches the regex, you need to “anchor” the regex on both sides, like this: regex=’^[0-9][0-9][_][0-9][0-9][_][0-9][0-9]$’ if [[ $incoming_string =~ $regex ]] then # Do awesome stuff here fi The ^ … Read more

Execute LambdaExpression and get returned value as object

Sure… you just need to compile your lambda and then invoke it… object input = 4; var compiledLambda = lambda.Compile(); var result = compiledLambda.DynamicInvoke(input); Styxxy brings up an excellent point… You would be better served by letting the compiler help you out. Note with a compiled expression as in the code below input and result … Read more

Performance of Expression.Compile vs Lambda, direct vs virtual calls

I didn’t find any answer, so here is the performance test: using System; using System.Diagnostics; using System.Linq.Expressions; using System.Reflection; using System.Reflection.Emit; namespace ExpressionTest { public interface IFoo { int Bar(); } public sealed class FooImpl : IFoo { public int Bar() { return 0; } } class Program { static void Main(string[] args) { var … Read more

tech