1

I want execute function c# like Convert.ToInt32([data]); DateTime.ParseExact("[data]", "dd MMM yyyy", provider from string. Before execute, I replace the data before

Example:

string aa = "Convert.ToInt32([data]);"
aa = aa.Replace("[data]", "1");
//var bb = result execute from aa

How to exexute like this??

Stfvns
  • 1,001
  • 5
  • 16
  • 42
  • 5
    Possible duplicate of [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) – Sani Huttunen Oct 19 '18 at 14:55
  • There is [a lot](https://www.google.co.uk/search?q=C%23+execute+string+as+code&oq=C%23+execute+string+as+code&aqs=chrome..69i57j69i58j0j69i60j0l2.3744j0j7&sourceid=chrome&ie=UTF-8) written about this topic already. What have you researched or tried so far? – ADyson Oct 19 '18 at 14:56
  • Possibile duplicate of https://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments – JazzmanJim Oct 19 '18 at 14:57
  • Take a look into this related topic : https://stackoverflow.com/a/540089/5916272 – YouneS Oct 19 '18 at 14:57
  • Thank all. I will try to understand. I'm still confused.. iI think not `int bb` but `var bb`. Bcuse not always int for result.. – Stfvns Oct 19 '18 at 15:06
  • 2
    @Stfvns -- Using my answer is a generic. So if you were expecting a string result, for example, you'd just change it to: `string bb = await CSharpScript.EvaluateAsync(aa);`. – Icemanind Oct 19 '18 at 15:10
  • @Icemanind for exacly I will don't know the variable result because i will many result like `int, date, decimal`. Because I will do looping from table.. before this question, I have another question before `https://stackoverflow.com/questions/52893131/how-to-insert-model-from-mapping-table`. Anybody can help me? – Stfvns Oct 19 '18 at 15:18
  • 2
    @Stfvns - `var` does not mean the type will change dynamically. The type is still determined by the compiler: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var – Broots Waymb Oct 19 '18 at 15:20
  • But I can't know the variable result. Could you take a look my another question `https://stackoverflow.com/questions/52893131/how-to-insert-model-from-mapping-table` please @BrootsWaymb – Stfvns Oct 19 '18 at 15:33
  • 1
    Will it always be this kind of conversion / string-parsing function you want to execute? Or will it be any arbitrary c# code? – ADyson Oct 19 '18 at 15:39
  • 1
    Check out (possible duplicate of) https://stackoverflow.com/questions/4181668/execute-c-sharp-code-at-runtime-from-code-file The CSharpCodeProvider class looks like promising. – Cody Hicks Oct 19 '18 at 15:41
  • I still can't implement all the answer all :( – Stfvns Oct 19 '18 at 16:03
  • 1
    @Stfvns `var` is evaluated at compile-time, not dynamically at run-time. However, you may be able to use `dynamic` instead. – Icemanind Oct 19 '18 at 16:57
  • 1
    Agreed, using dynamic could be a way forward – ADyson Oct 19 '18 at 17:51

2 Answers2

4

Roslyn is the easiest way to do this:

int bb = await CSharpScript.EvaluateAsync<int>(aa);
Icemanind
  • 47,519
  • 50
  • 171
  • 296
-6

Seems like the example ins't too clear. I think you could use String Interpolation to accomplish what you are looking for.

Like: $"Coin flip: {(rand.NextDouble() < 0.5 ? "heads" : "tails")}"

Here is the reference to MSDN documentation. https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/string-interpolation

Andre Gamboa
  • 64
  • 1
  • 8