12

I am looking for an open source library to parse and execute formula/functions in C#.

I would like to create a bunch of objects that derive from an interface (i.e. IFormulaEntity) which would have properties/methods/values and the allow a user to specify formulas for those objects.

For example, I might have

public class Employee : IForumulaEntity
{ 
      public double Salary { get; set; }
      public void SendMessage(string message)
}

Then allow an application user to write something like;

Employee person = <get from datasource>
if (person.Salary > 1000)
    person.Salary += 1000;

person.SendMessage("Hello");

This "looks like C#" but it would be a simplified programming language. I know it's alot to ask. I would expect my users to be reasonably capable (i.e. can write their own Excel formulas).

2
  • (why do I see this question as **DELETE MY ACCOUNT** at main page?) Commented Oct 7, 2010 at 18:44
  • With over 5000 reputation and 2 gold badges, you should really know better than to try and purge your question content like that. Did you know you can flag them for moderator attention? Just say you want your account deleted. They'll take care of the rest for you. Just editing with that message isn't going to make a moderator notice you. Commented Oct 7, 2010 at 18:47

5 Answers 5

6

Look into the Rules Engine functionality that is part of Windows Workflow Foundation.

This is not a place one would think to look. However, as part of the work they did to produce a UI that allows a Workflow developer to use configurable rules and expressions, they've come up with a way to do the same thing in your own application, even if you are not using Workflow at all. Their documentation includes examples of hosting this functionality in a Windows Forms application.

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

5 Comments

+1. Yes this is exactly the sort of thing Im looking for [msdn.microsoft.com/en-us/library/aa480193.aspx] except I want to allow my user to write the rules.
That's what I meant - it does allow the user to write the rules. You can host the rules designer in your own application, and the user can write the rules. You can then apply the user-written rules definition against your data at runtime.
As usual, Microsoft provides the tools. That's why I love them :)
Be very careful using V3 considering V4 is pretty much a rewrite *(listen to the recent Brian Noyes DNR for a good lowdown). If looking for a book, the Essential WF book is decent, but V3-based.
True, but do you happen to know to what extent the Rules Engine has changed?
6

I've used ANTLR, FSLEX/FSYACC and Managed Babel. All do what you are asking, and while all are free, only the first is open source.

Antlr: http://antlr.org/

FSLEX: http://www.strangelights.com/fsharp/wiki/default.aspx/FSharpWiki/fslex.html

Managed Babel: http://msdn.microsoft.com/en-us/library/bb165963.aspx

Comments

1

You could use cs-script. This allows you to execute c# code, that is maybe more then you want, but why not just stick to c# instead of creating your own c#-like syntax. It is possible to integrate cs-script into your application as DLL and you can allow it to execute c# code that is not part of a class so that users can just write the few statements they need.

Comments

0

Spring expressions are very powerful, and less of an all or nothing than e.g. WF

(The engine is ANTLR based, and the expressions bit, while it pulls in a decent sized lib, doesnt yank in all of Spring.NET)

3 Comments

And yes, I pride myself on the timeliness of my answers!
AH! Stalker! I ended up rolling my own, because the requirement is reasonbly specific. See www.twipler.com/experiment
Guilty as charged! Yes, that looks a long way from WF!
0

While I'm beating this to death, Dynamic LINQ plays in this space (specifically the C# like syntax). Obviously be careful that it's requirements (emission/compilation of code) are supported in your context.

Comments

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.