0

i dont know if it exist or if it exist how its called. Can you generate code/ run code from text?

Let me explain clearly Ex:

string myFunction = "Console.WriteLine("Hello world");";

public static RunMyFunction()
{
    myFunction;
}

I know these lines of codes are actually wrong but its just to give an example.

Thank you

Well I searched google but i dont event know how it is named. I really come to you guys as final step.

CSharpMan
  • 11
  • 2
  • search for CodeDOM or T4, which are tools to create source-code at runtime. – MakePeaceGreatAgain Apr 05 '23 at 09:52
  • @CSharpMan There are two different problem: are you want to only execute this code, or you want do something else with code? – RezaNoei Apr 05 '23 at 10:24
  • As indicated by @MakepeaceGreatAgain, CodeDOm is the solution. refer to Is it possible to dynamically compile and execute C# code fragments :https://www.google.com/search?q=dynamic+compilation+of+code+C%23&client=firefox-b-d&ei=v1gtZKwc86qR1Q-72K_oCQ&ved=0ahUKEwjs96H7zpL-AhVzVaQEHTvsC50Q4dUDCA4&uact=5&oq=dynamic+compilation+of+code+C%23&gs_lcp=Cgxnd3Mtd2l6LXNlcnAQAzIFCCEQoAEyCAghEBYQHhAdOgoIABBHENYEELADSgQIQRgAUMYGWPUQYLcVaAFwAXgAgAFyiAHGAZIBAzEuMZgBAKABAcgBCMABAQ&sclient=gws-wiz-serp – Graffito Apr 05 '23 at 11:19
  • As indicated by @MakePeaceGreatAgain, CodeDOM is the solution, see `Is it possible to dynamically compile and execute C# code fragments?` https://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments – Graffito Apr 05 '23 at 11:22
  • @RezaNoei no i made an app with dynamically created panels that let the user build a request but it cannot actually work as some part of code are text. – CSharpMan Apr 05 '23 at 15:05
  • ok a big thank you to all to pointed me in the right direction i will look into it!!! Appreciate – CSharpMan Apr 05 '23 at 15:06
  • Taking c# code as text from the user and executing it is rarely a good idea. It is a security nightmare, regular users will have no idea what to do, and programmers will be irritated by the poor development environment. I would suggest either creating a plugin system, or take a much more limited type of input, like regex, or a mathematical expression. – JonasH Apr 06 '23 at 10:04
  • @JonasH : Security issues are not a problem, when you provide to the user a desktop software where user may write some code for its own purpose (assuming that he can also create other programs on its computer). – Graffito Apr 10 '23 at 00:13

0 Answers0