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 g
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, IList>((s,q) => (from contact in q where contact.Name == s select contact).ToList());";
var func = (Func, IList>)Evaluator.Evaluate(s);
var result = func("John", myQueryableOfContactsFromNHibernate);
}
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);