expression-trees

Serialize expression tree

非 Y 不嫁゛ 提交于 2019-12-03 18:57:55
问题 I'm doing a distributed system in c# and have encountered a barrier. I need to be able to serialize Predicate with type Predicate<ICollection<IEntity>> p = (entities => entities.OfType<Person>().Count() <= 3); I belive this is not possible in .net so my question is if there exists any frameworks that can do the trick. I've already tried a couple of frameworks, but keep running into the problem that their are not able to serialize predicates that takes a collection or list Hope anyone knows a

Dynamic MemberExpression

别等时光非礼了梦想. 提交于 2019-12-03 17:50:25
问题 I am wanting to create a MemberExpression knowing only the field name; eg: public static Expression<Func<TModel, T>> GenerateMemberExpression<TModel, T>(string fieldName) { PropertyInfo fieldPropertyInfo; fieldPropertyInfo = typeof(TModel).GetProperty(fieldName); var entityParam = Expression.Parameter(typeof(TModel), "e"); // {e} var columnExpr = Expression.MakeMemberAccess(entityParam, fieldPropertyInfo); // {e.fieldName} var lambda = Expression.Lambda(columnExpr, entityParam) as Expression

Why is JIT_MethodAccessAllowedBySecurity taking so much time?

半腔热情 提交于 2019-12-03 17:50:07
问题 I'm working on a C# application that allows users to basically import tables of data, and then enter their own formulas in a mini-language to compute new columns from the underlying data. These formulas are compiled into LINQ expression trees in the engine, which the .NET 4.0 expression tree library then presumably compiles into IL so they can be executed. We've started using our engine for some high-volume ticking data recently, and we're finding the speed of these compiled expression trees

Convert func to predicate using reflection in C#

ⅰ亾dé卋堺 提交于 2019-12-03 17:03:20
I'm basically trying to do this , but I don't know what T will be, so I'm building things up using Reflection and Expression trees. // Input (I don't know about "Book") Type itemType = typeof(Book); // Actual Code // Build up func p => p.AuthorName == "Jon Skeet" ParameterExpression predParam = Expression.Parameter(itemType, "p"); Expression left = Expression.Field(predParam, itemType.GetField("AuthorName")); Expression right = Expression.Constant("Jon Skeet", typeof(string)); Expression equality = Expression.Equal(left, right); Delegate myDelegate = Expression.Lambda(equality, new

How to convert CIL to a LINQ Expression Tree [closed]

廉价感情. 提交于 2019-12-03 16:32:33
Has any work been done on the direct conversion of CIL to LINQ expression trees? This would include class libraries, blogs, books, academic papers, etc. What are the known mismatches between CIL and LINQ Expression API? I have seen a project that significantly extended the Expression API to support an almost complete mapping between C# and LINQ Expressions. What work arounds would be suggested to accommodate any of these mismatches? Is there something fundamentally wrong about converting directly from CIL to LINQ Expressions? If so, why? 来源: https://stackoverflow.com/questions/12824201/how-to

Why are expression trees safer than reflection?

人盡茶涼 提交于 2019-12-03 15:59:56
问题 In this answer to the question of the fastest way to determine if a property contains a given attribute, user Darin Dimitrov posited that expression trees are safer than reflection. Is this true, and if so, why is it true? 回答1: Because when you search for your field (as in that question) you use string representation "Id" . Once it is changed your reflection will collapse. What Darin suggests is static typing: Expression<Func<Program, int>> expression = p => p.Id; You see that? This is

How Does Queryable.OfType Work?

余生长醉 提交于 2019-12-03 15:41:56
Important The question is not "What does Queryable.OfType do , it's "how does the code I see there accomplish that?" Reflecting on Queryable.OfType, I see (after some cleanup): public static IQueryable<TResult> OfType<TResult>(this IQueryable source) { return (IQueryable<TResult>)source.Provider.CreateQuery( Expression.Call( null, ((MethodInfo)MethodBase.GetCurrentMethod()).MakeGenericMethod( new Type[] { typeof(TResult) }) , new Expression[] { source.Expression })); } So let me see if I've got this straight: Use reflection to grab a reference to the current method (OfType). Make a new method,

Linking a .NET Expression Tree into a new assembly

故事扮演 提交于 2019-12-03 15:38:22
I'm trying to write my own toy My Toy Language -> MSIL compiler in order to get a better understanding of how compilers work. I got the parsing and lexing working, I have built the expression trees and using the System.Linq.Expressions expression tree API, I have a working interpreter. Now I would like to emit some real MSIL assemblies. The problem is, I can't figure out how to actually build these assemblies. The MethodBuilder class only accepts raw MSIL method bodies, so I have to get the raw MSIL of my expression tree. Calling Expression.Compile() returns a working delegate but I'm not able

Building Expression Tree for string.Contains [duplicate]

寵の児 提交于 2019-12-03 15:23:55
This question already has answers here : How do I create an expression tree to represent 'String.Contains(“term”)' in C#? (4 answers) I'm struggling to build an expression tree so I can dynamically do filtering on some data. I have come up with this, but it fails at the var lambda = line foreach (var rule in request.Where.Rules) { var parameterExpression = Expression.Parameter(typeof(string), rule.Field); var left = Expression.Call(parameterExpression, typeof(string).GetMethod("ToLower", Type.EmptyTypes)); var right = Expression.Constant(rule.Data.ToLower()); var method = typeof(string)

LINQ expressions. Variable 'p' of type referenced from scope, but it is not defined

感情迁移 提交于 2019-12-03 15:11:45
问题 I'm building a LINQ query dynamically with this code. It seems to work, but when i have more than one searchString in my search, (so when multiple expressions are added, i get the following error: Variable 'p' of type referenced from scope, but it is not defined** I guess i can only define /use p once. But, if so, i need to alter my code a bit. Can anyone point me in the right direction here? if (searchStrings != null) { foreach (string searchString in searchStrings) { Expression<Func<Product