注射可变进Mono.CSharp.Evaluator(运行时编译的字符串从LINQ查询) [英] Injecting a variable into the Mono.CSharp.Evaluator (runtime compiling a LINQ query from string)
问题描述
我使用的Mono.CSharp库发出代码。继SO另一个问题(http://stackoverflow.com/questions/3407318/mono-compiler-as-a-service-mcs)我设法Mono.CSharp在微软CLR正确评估。
要在我的应用程序增加了灵活性,我想能够自定义在运行时的查询 - 通过允许用户提供LINQ查询作为获取解析和执行时访问数据库字符串
鉴于这一基本代码片段:
的IQueryable<联系与GT ;接触= GetContacts();
查询字符串=从通讯录
,其中contact.Name == \接触name\
:选择联系人;
VAR queryableResult = Mono.CSharp.Evaluator.Evaluate(查询);
我怎样才能'注入'被评定为部分可变进Mono.CSharp.Evaluator接触查询的?我要对这个正确的方式?最后,我要么需要从'查询'字符串所产生的表达或IQueryable的。
我觉得你有几个选项:
-
使用静态或ThreadStatic变量交换呼叫者和你基于字符串代码之间的数据:
命名空间myns名字
{
公共MyClass类
{
[ThreadStatic] //线程静态所以数据特定于调用线程
公共静态字符串MyEnumerableVariable;
公共无效DoSomething的()
{
Evaluator.ReferenceAssembly(Assembly.GetExecutingAssembly()); (使用myns名字;)
Evaluator.Run
//运行动态代码
变种s = @在MyNs.MyClass.MyEnumerableVariable回报(从接触的地方contact.Name == 约翰,选择联系人).ToList();;
Evaluator.Evaluate(S);
}
}
} -
从你的字符串代码返回一个代表:
公共无效DoSomething的()
{
//运行动态代码
变种s = @回报新Func键<字符串的IQueryable< MyNs.Model.Contact>中的IList>((S,q)= >(从q其中contact.Name ==•选择洽洽).ToList());;
VAR FUNC =(Func键<字符串的IQueryable< MyNs.Model.Contact>中的IList>)Evaluator.Evaluate(S);
VAR的结果= FUNC(约翰,myQueryableOfContactsFromNHibernate);
}
- 转至完全成熟的路线。
查询字符串=的String.Format(
@使用(VAR DC =新的DataContext())
{
回报率(自直流接触。联系人那里contact.Name =={0}选择联系人).ToList();
},约翰);
VAR的结果= Mono.CSharp.Evaluator.Evaluate(查询);
I'm using the Mono.CSharp library to emit code. Following another question on SO (http://stackoverflow.com/questions/3407318/mono-compiler-as-a-service-mcs) I managed to get Mono.CSharp evaluating correctly on the Microsoft CLR.
To add flexibility in my app I'd like to be able to customize a query at runtime - by allowing the user to provide a LINQ query as a string that gets parsed and hits the database when executed.
Given this basic snippet of code:
IQueryable<Contact> contacts = GetContacts();
string query = "from contact in contacts
where contact.Name == \"name\"
select contact";
var queryableResult = Mono.CSharp.Evaluator.Evaluate(query);
How can I 'inject' the contacts variable into the Mono.CSharp.Evaluator to be evaluated as part of the query? Am I going about this the right way? In the end I either need the resulting Expression or the IQueryable from the 'query' string.
I think you have a few options:
Use static or ThreadStatic variables to exchange data between the caller and you string based code:
namespace MyNs { public class MyClass { [ThreadStatic] // thread static so the data is specific to the calling thread public static string MyEnumerableVariable; public void DoSomething() { Evaluator.ReferenceAssembly(Assembly.GetExecutingAssembly()); Evaluator.Run("using MyNs;") // run the dynamic code var s = @"return (from contact in MyNs.MyClass.MyEnumerableVariable where contact.Name == ""John"" select contact).ToList();"; Evaluator.Evaluate(s); }
} }
Return a delegate from your string code:
public void DoSomething() {
// run the dynamic code var s = @"return new Func<string, IQueryable<MyNs.Model.Contact>, IList>((s,q) => (from contact in q where contact.Name == s select contact).ToList());"; var func = (Func<string, IQueryable<MyNs.Model.Contact>, IList>)Evaluator.Evaluate(s); var result = func("John", myQueryableOfContactsFromNHibernate);
}
- Go the full blown route.
string query = string.Format(
@"using (var dc = new DataContext())
{
return (from contact in dc.Contacts where contact.Name == ""{0}"" select contact).ToList();
}", "John");
var result = Mono.CSharp.Evaluator.Evaluate(query);
这篇关于注射可变进Mono.CSharp.Evaluator(运行时编译的字符串从LINQ查询)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!