C # implement eval Functions

Source: Internet
Author: User

Javascript has an eval function that can dynamically execute code. But not in C!

The following code is the core code extracted from my project. In fact, it is very simple to compile --> Generate an assembly --> reflection call.

Learn from beginners!

// If execution is successful, null is returned; otherwise, error text is returned.
String strerr;

// The code execution result is returned in the form of a string, because only the string result is required in my project, you can follow the instructions to modify it yourself. Www.elivn.com
String strresult = commanddebugger. evaluator. eval ("Your codes", out strerr );

Download Code:

------------------------------------------------------------------

Thank you for your comments. I am new to you. Please forgive me. In fact, this function is tailored according to my program.

Ironpython has heard of it, but it has never been used and is not familiar with it.

In my project, there is such a function that can dynamically execute a piece of code and return a value.

For example, to view the value of a static variable in the current context, you can write it as follows:

String strerr;

String strresult = commanddebugger. evaluator. eval ("staticclass. Value", out strerr );

You can also use include C:/Windows/system. dll and other statements to import the assembly.
Using system. xml;
Print: Output statement results ..

If it is simply to implement an eval function, I think this method is certainly not very good, but according to the requirements of my program, haha, it should be a good deal... Please kindly advise !!

The following classes are mainly used for implementation.

Csharpcodeprovider
Compilerparameters
Compilerresults

Javascript has an eval function that can dynamically execute code. But not in C!

The following code is the core code extracted from my project. In fact, it is very simple to compile --> Generate an assembly --> reflection call.

Learn from beginners!

// If execution is successful, null is returned; otherwise, error text is returned.
String strerr;

// The code execution result is returned in the form of a string, because only the string result is required in my project, you can follow the instructions to modify it yourself. Www.elivn.com
String strresult = commanddebugger. evaluator. eval ("Your codes", out strerr );

Download Code:

------------------------------------------------------------------

Thank you for your comments. I am new to you. Please forgive me. In fact, this function is tailored according to my program.

Ironpython has heard of it, but it has never been used and is not familiar with it.

In my project, there is such a function that can dynamically execute a piece of code and return a value.

For example, to view the value of a static variable in the current context, you can write it as follows:

String strerr;

String strresult = commanddebugger. evaluator. eval ("staticclass. Value", out strerr );

You can also use include C:/Windows/system. dll and other statements to import the assembly.
Using system. xml;
Print: Output statement results ..

If it is simply to implement an eval function, I think this method is certainly not very good, but according to the requirements of my program, haha, it should be a good deal... Please kindly advise !!

The following classes are mainly used for implementation.

Csharpcodeprovider
Compilerparameters
Compilerresults

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.