Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7706721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:11:11+00:00 2026-06-01T00:11:11+00:00

I have a situtaiton where I read the bussiness logic and replace the variables

  • 0

I have a situtaiton where I read the bussiness logic and replace the variables with actual values and then I need to evaluate it to get result. I am currently using bcParser to do it and it works just fine for all the logic which is written like excel format.

The curve ball thrown at me is that, the if condition will not be like excel if(cond, true, false) rather it will be like C# where the if (cond) { true; } else { false;}, this makes more sense and easy to maintain. Since I replace all the variables with value before hand, all I have to do is evaluate it. Currently I am solving this problem by exporting the logic to c# methods and using reflection I am evaluating it and it also works.

I am wondering is there any other option, I do not want to write code for each if condition and would like to evaluate it on the run time. I was wondering if I should able to create a token parser of some sort and call C# native expression evalution and perform the calculation. I haven’t gone into understanding expresion trees, it seems it is possible with that approach. before I go there, I would like to know is it possible at all?
Thanks,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:11:12+00:00Added an answer on June 1, 2026 at 12:11 am

    Yes!

    The key is using the System.Linq.Expressions namespace. You can build up an expression tree programmatically, either in your code or by modifying your parser then compile it into a Delegate. This API compiles your Delegate inside of a DynamicAssembly which means that your compiled expressions can be unloaded from memory by the garbage collector when you completely dereference them.

    Here is a very simple example:

    var b = true;
    Func<bool> condition = () => b;
    Action trueExpression = () => { Console.WriteLine(true); };
    Action falseExpression = () => { Console.WriteLine(false); };
    
    var e = Expression.Condition(
        Expression.Invoke(Expression.Constant(condition)),
        Expression.Invoke(Expression.Constant(trueExpression)),
        Expression.Invoke(Expression.Constant(falseExpression)));
    
    var λ = Expression.Lambda(e).Compile();
    
    b = true;
    λ.DynamicInvoke();
    
    b = false;
    λ.DynamicInvoke();
    

    This produces the output:

    True
    False
    

    The step where the expression is compiled into a Lambda can be a significant performance hit, you will want to come up with a caching strategy for your compiled lambdas. It’s well worth it though, calling the compiled lambda using DynamicInvoke is very fast. Almost as fast as if you had pre-compiled it. This technique is significantly faster than using CodeDom code generation (which requires a whole another process to do the compilation) and it has the major benefit of producing unloadable assemblies.

    The only limitation to this is that you cannot create Types with this API. You have to limit yourself to expressions and statements. It’s quite powerful however, this is the magic guts of the DLR.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation where I need to read a file full of numbers
I have a situation where I need to connect multiple DBs to get all
I have situation in which I read a record from a database. And if
I have a situation where I want my program to read in some numbers
My current situation is: I have to read a file and put the contents
i have situation like this: class IData { virtual void get() = 0; virtual
I have situation where I need to change the order of the columns/adding new
We have a situation where users are allowed to upload content, and then separately
I have a situation where I need to use some strings temporarily but I've
I have a situation where I need to make some data available for reading

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.