3

I'm modeling some financial products and each product has his own pricing formula. In the application I would like to allow the end-user create his own product with the formula. And this formula can be used by my application to price the product.

Something like :

Formula as string = "f(x) = x * 2"
Dim Result as double = call(Formula, 1)

I know this is possible in Matlab :

f="@(x)(x*2)"; 
Result = feval(f,1);

I wrote a class in Matlab that implements this feature and integrated it in VB.Net project, but every function takes 4700 times the execution of the same function directly written in VB.Net which is not affordable regarding the business need.

Is that possible in .Net ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Thomas Carlton
  • 5,344
  • 10
  • 63
  • 126
  • you could look at the [DynamicMethod Class](http://msdn.microsoft.com/en-us/library/system.reflection.emit.dynamicmethod(v=vs.110).aspx) but it might be more involved than you want. Years ago I had a similar need for a client and found that about 80% of the time they were using the same SET of functions over and over just with different args. I ended up building a set of these, then let them pick a formula by name and specify the arg values. Then use a CASE statement to send the args to the right function. NET likely has a cleaner way. – Ňɏssa Pøngjǣrdenlarp Jan 26 '14 at 13:43
  • Actually this is what I want to avoid : Case statement, because the need may involve completely different formulas each time... – Thomas Carlton Jan 26 '14 at 13:52
  • What if they wrote the functions in VB.NET (Express edition is free)? They could produce a class library and then tell you to use it. – John Saunders Jan 26 '14 at 13:57

1 Answers1

0

You can look into MEF, so your end users would provide DLL modules in a certain format (see the link I mentioned), which would later be discovered in your program and executed at any given time.

Or use a math parser:

But I feel that approach #1 would be more flexible.

Community
  • 1
  • 1
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151