2

Trying to figure out how to exploit the CSharpCodeProvider to compile string data at runtime.

In xml, i have what will amount to a predicate condition stored in a string.

I want to be able to, within the current context, execute and get the result of these statements.

for instance when <Condition>Value=="ABCD1234"</Condition> is read and executed, it should look in the current object scope for value and return true or false based on the evaluation of the statement.

I think i'm going the right direction with CSharpCodeProvider, however all the examples i can find relate to compiling to a .DLL or .EXE, i really just want to compile in memory so i can perform the comparison and return true or false to another part of the app.

Any ideas as to how best to approach this? is CSharpCodeProvider even the best tool for the job?

4
  • 1
    What do you mean by "current object scope"? The result of CSharpCodeProvider will always be an assembly, which means your "snippet" must live inside a method contained in a class. So "current object scope" will have to be instrumented yourself as parameters to the enclosing method that you create. Commented Oct 19, 2010 at 16:34
  • Not sure I get this, it is the difference between an interpreter and a compiler. Sure, CodeDom will make it a lot quicker. Not because of strings. Commented Oct 19, 2010 at 16:44
  • Well, i just wanted to make sure that when i say Value="stuff" Value will be a property of an instantiated object. Commented Oct 19, 2010 at 17:24
  • Yeah, but if you were to write that class manually to be compiled by C# afterwards, how do you access the Value property without additional reference (e.g. obj.Value)? You cannot inherit in this case, so you have to write same-named properties in your class which delegate the calls to the original instance. Commented Oct 19, 2010 at 17:26

3 Answers 3

1

I would highly recommend using FLEE for something like this. It doesn't know native C# but it has the ability to parse and execute a reasonable subset of functions that looks like it'd be suitable for your use.

I've used FLEE on many occasions and found it to be easy to use and plenty fast enough.

Sign up to request clarification or add additional context in comments.

1 Comment

I'll play with this a bit. It got me thinking though. Turns out that I forgot that we use Lex/YAAC in another project. I'll play with this. Thanks.
1

I would personally rather create a small DSL (domain-specific language) instead of compiling such strings. The problem is that mixing this together into some source code that is compiled opens the door to injection attacks, and checking the syntax is difficult as well. Creating a small parser for simple expressions and predicates is actually pretty easy, so if you don't depend on LINQ and whatever to work in your conditions then you may be better of with the DSL route.

To get an impression of what I'm talking about, I invite you to have a look at my GOLD Parser Engine which is made for jobs like this: http://code.google.com/p/bsn-goldparser/

The output of the parsing process is a semantic AST (abstract syntax tree), which can then be written to perform an "interpreted" evaluation on the fly or to create MSIL code, so that you get the benefit of fast compiled (JITed) code as well with full control over what is happening.

Comments

0

CSharpCodeProvider might be overkill. If your conditions will be basic logical operators (e.g. equals, greater/less than) you may be better off building a few delegates and writing a simple parser to execute the condition. Your parser will determine which operator is used and execute the appropriate delegate:

delegate bool equalsDel(string s1, string s2)
equalsDel = (s1, s2) => {return sq == s2;}
//Get your strings from the file and invoke the delegate
return equalsDel(s1, s2);

Look into the Visitor pattern for ideas on how to write a parser/Visitor that will determine the operator and execute the appropriate delegate. You might be able to get creative with Func<T> to create an efficient system with not much coding.

2 Comments

They will be basic logic operations, but i was hoping to also be able to exploit the nesting/grouping inherent in most languages. I suppose if i go that route i'll just have to hand-write the grouping constructs.
If you use a proper parser (e.g. someting that includes a tokenizer followed by LL/LALR parsing using a well-defined grammar) you get that "for free". ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.