C# execute variable as code at runtime

  • Thread starter Thread starter Jimm64
  • Start date Start date
J

Jimm64

Guest
Is there a way (reflection or some other method) to execute code in contained within a variable at run time?Another language Ive used (called Cache) would allow you to use indirection to do this... for example, you could have a variable like this: Set A = " D = B + C "in Cache. You could then do this X @A which would then execute the statement contained in the variable A as if it was part of the compiled code.

I have the need to be able to do Logical expressions...ex. if ((A == B) && (C == D)) at run time instead of compile time.... where this part of the statement (A == B)&&(C == D) is not known until run time.




OR if I could create a method or whole C# file dynamically using reflection and then calling a method in that file might work as well....

For example, would this general idea be possible:

  1. Have a known method name... we will call it ReturnTrueOrFalse for this example
  2. Generate the method which will return a boolean value
  3. Generate the code at run time... the code to generate being stored in one or more string arrays(??)
  4. Compile the code at runtime
  5. Execute the code at run time after the code was generated and compiled.

Assuming it isnt possible to execute code contained within a variable as Ive done in Cache, is this possible?


Thanks!
Jim

Continue reading...
 
Back
Top