Performance of compiled-to-delegate Expression

This is pretty strange for such a huge overheard. There are a few things to take into account. First the VS compiled code has different properties applied to it that might influence the jitter to optimize differently. Are you including the first execution for the compiled delegate in these results? You shouldn’t, you should ignore … Read more

How to use Expression to build an Anonymous Type?

You’re close, but you have to be aware that anonymous types don’t have default constructors. The following code prints { Name = def, Num = 456 }: Type anonType = new { Name = “abc”, Num = 123 }.GetType(); var exp = Expression.New( anonType.GetConstructor(new[] { typeof(string), typeof(int) }), Expression.Constant(“def”), Expression.Constant(456)); var lambda = LambdaExpression.Lambda(exp); object … Read more

How set value a property selector Expression

This works: The following helper method converts a getter expression into a setter delegate. If you want to return an Expression<Action<T,TProperty>> instead of an Action<T,TProperty>, just don’t call the Compile() method at the end. Note: The code is from Ian Mercer’s blog: http://blog.abodit.com/2011/09/convert-a-property-getter-to-a-setter/ /// <summary> /// Convert a lambda expression for a getter into a … Read more

Is there a way to create a delegate to get and set values for a FieldInfo?

As Peter Ritchie suggested, you can compile your own code at runtime. The method will be compiled as soon as you invoke the delegate for the first time. So the first call will be slow, but any subsequent call will be as fast as you can get in .NET without unmanaged pointers/unions. Except for the … Read more

Generate EF orderby expression by string [duplicate]

Using reflection and expression-trees you can provide the parameters and then call OrderBy function, Instead of returning Expression<Func<Task, T>> and then calling OrderBy. Note that OrderBy is an extension method and has implemented in both System.Linq.Enumarable and System.Linq.Queryable classes. The first one is for linq-to-objects and the latter is for linq-to-entities. entity-framework needs the expression … Read more

How to Combine two lambdas [duplicate]

To complete Eric’s answer, using the new ExpressionVisitor introduced in .NET 4 rather than a custom rewriter: internal class ParameterReplacer : ExpressionVisitor { private readonly ParameterExpression _parameter; protected override Expression VisitParameter(ParameterExpression node) { return base.VisitParameter(_parameter); } internal ParameterReplacer(ParameterExpression parameter) { _parameter = parameter; } } class Program { static void Main(string[] args) { Expression<Func<string, bool>> … Read more

Does Json.NET cache types’ serialization information?

Yes, it does. Json.NET caches type serialization information inside its IContractResolver classes DefaultContractResolver and CamelCasePropertyNamesContractResolver. Unless you specify a custom contract resolver, this information is cached and reused. For DefaultContractResolver a global static instance is maintained internally that Json.NET uses whenever the application does not specify its own contract resolver. CamelCasePropertyNamesContractResolver, on the other hand, … Read more

Given a type ExpressionType.MemberAccess, how do i get the field value?

[updated for clarity] First; cast the Expression to a MemberExpression. A MemberExpression has two things of interest: .Member – the PropertyInfo / FieldInfo to the member .Expression – the expression to evaluate to get the “obj” for the .Member i.e. if you can evaluate the .Expression to “obj”, and the .Member is a FieldInfo, then … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)