How to implement the following function/method?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

What's the best easy way to have a function/method:

1. Has a string paramter, the value of string is C#/VB.Net express.
2. The function evaluate the express and return the value.
 
ydbn said:
What's the best easy way to have a function/method:

1. Has a string paramter, the value of string is C#/VB.Net express.
2. The function evaluate the express and return the value.

The question is what do you want to evaluate?


string YourFunction (string strParameter)
{
string result;

// Code for something to evaluate

return result;
}
 
the parameter will have some value like:

"100>10 && 100<1000 ? 1 : 0"
"'a' in ('a','b','c')"
etc.
 
What's the best easy way to have a function/method:

1. Has a string paramter, the value of string is C#/VB.Net express.
2. The function evaluate the express and return the value.
I don't know if this is the best/easiest way, but it is a way.

The parameter string has to be a complete compilable program, or at
least something that can be turned into a complete program easily.

1 If needed turn the parameter string into a string containing a
complete program.
2 Save the complete program string to a file.
3 Invoke the C# compiler to compile the program file into an exe file.
4 Invoke the newly created exe file.
5 Retrieve and return the result.

rossum
 
ydbn said:
the parameter will have some value like:

"100>10 && 100<1000 ? 1 : 0"
"'a' in ('a','b','c')"
etc.

ok. but be more precisely. I'm little bit confused right now what do you really want? :D
Do you need algorithm for extracting some values from given parameter or something else?
 
rossum said:
I don't know if this is the best/easiest way, but it is a way.

The parameter string has to be a complete compilable program, or at
least something that can be turned into a complete program easily.

1 If needed turn the parameter string into a string containing a
complete program.
2 Save the complete program string to a file.
3 Invoke the C# compiler to compile the program file into an exe file.
4 Invoke the newly created exe file.
5 Retrieve and return the result.

If you use CSharpCodeProvider, you don't need to write it to file or
convert it into an executable - just compile it from memory and into
memory. I'm doing that all the time at the moment, and it works well.
 
ydbn said:
What's the best easy way to have a function/method:

1. Has a string paramter, the value of string is C#/VB.Net express.
2. The function evaluate the express and return the value.

If you can live with JavaScript syntax, then JScript has a nice
eval function for doing this.

And a JScript class with a wrapper method can be called
from C#.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top