Is there a way that I can dynamically determine the body of a Predicate from a string containing code?

This is probably a stupid question, but here. I would like to be able to dynamically build a <T> predicate from a row processed from a VARCHAR database column, or any row, for that matter. For example, let's say that the column in the database contains the following row:

return e.SomeStringProperty.Contains("foo");

These code / line values ​​will be stored in the database, knowing what are the possible properties of the common "e", and knowing that they need to return a boolean value. Then, in a magical, beautiful, fantastic world, the code could be executed without knowing what a predicate is, for example:

string predicateCode = GetCodeFromDatabase();
var allItems = new List<SomeObject>{....};
var filteredItems = allItems.FindAll(delegate(SomeObject e) { predicateCode });

or giardiasis:

var filteredItems = allItems.FindAll(e => [predicateCode]);

, , , , , , Reflection.Emit, FindAll <T> ( /)?

+3
4

# VB .NET Framework:

# CodeDom Provider

, ( AppDomain). , , . .

System.Reflection.Emit - API CLR. , , , CIL.

LINQ - ( CIL), .

" ", CLR ( DLR), IronPython. , .

+1

emit, .

, ScottGu PDC CLI- .net, Ruby eval, URL-, . commnity wiki, , , .

0

linq, , , , .

: , , , "-".

, : orders.Findall(o => o.Items.Exists(i => i.Name == "coca cola"))

In dynamic linq, I did not find a way to do this, so I started with CodeDomProvicer. I created a new one Typeusing a method that contains my dynamically built method FindAll:

public static IList Filter(list, searchString)
{
   // this will by dynamically built code
   return orders.Findall(o => o.Items.Exists(i => i.Name == "coca cola"));
}

when I try to build this assembly:

CompilerResults results = provider.CompileAssemblyFromSource(parameters, sb.ToString());

I get an error message:

Invalid expression term ">"

Why can't the compiler compile a predicate?

0
source

Source: https://habr.com/ru/post/1703526/


All Articles