0

I try to convert a part of code from VBScript to C#. I have these lines :

string replacementMatch = ....; 

...

replacementMatch = Eval(replacementMatch); 

OR

Execute(replacementMatch); 

I can't find the equivalent in C#.

5 Answers 5

4

The Eval function evaluates an expression and returns the result. You will not find an equivalent in statically typed languages such as C# and VB.NET. There are some libraries that provide such functionality such as flee.

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

Comments

0

You can't find exactly the same functionality but you can use CodeDomProvider to compile an assembly from generated code and use Reflection to invoke methods from it.

Comments

0

You can add a reference to Microsoft Script Control and call its Eval function in c#. See this answer.

How can I evaluate the result of a boolean expression in string format on runtime in C#?

Comments

0

You will not find an equivalent. You could use the C# compiler, but it's never a good idea to execute arbitrary code in you applications. Consider your requirements, and see if writing a simple parser would be feasible. Check out System.Linq.Expressions in that case.

Comments

0

In the documentation VBScript you can find that "Eval" and "Execute" arguments are evaluated as "as a VBScript expression". So in short words there are no C# equivalents.

In general you should review the vb code and consider rewrite it in C# to not require evaluating script at the runtime.

If you really need to create statements at runtime and evaluate them, you could try third party tools - like "http://www.codeproject.com/KB/dotnet/evaluator.aspx" (although I didn't used this particular tool - I could see very few situations when this is really needed).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.