I am making a mathematical Integration app in xaml and C# and for that I need to cast the user's input string representing a lambda expression in to a lambda expression or a function delegate. Is there any way to do so?
1 Answer
You can't directly cast a string to a Lambda expression in C#. You'll need to compile it somehow into executable code. There are several possibilities:
- Create C# code and Emit a dynamic assembly
- Create a CodeDom graph and compile it
- Look into Roslyn, which is the base for the upcoming C# compiler.
Depending on the complexity of the user's expressions, you might be better off interpreting them yourself.
4 Comments
Richard Deeming
Don't forget Mono.CSharp, which has been around a lot longer than Roslyn! :)
user3411639
Can you pls tell me how to do so using any one method as I am quite new to C#! I tried Mathos parser but this library cannot be downloaded for windows 8 store apps where as it works fine with windows form.
Jim Mischel
@user3411639: Without more information about what your user's input is, I can't give you more specific recommendations. I would suggest that you read up on the options, select one that you think would be appropriate, and try to implement your solution. If you run into trouble, post another question on Stack Overflow. Include in it information about the expected user input, what you expect your program to do, what you've done, and specific questions about how to make it work.
user3411639
My input particularly is in the following format x=>x*x. This is a string and I want to convert it into a lambda exp or a delegate of type double would also suffice. Pls help