How to insert value in dynamic compilation?

Hello,

There is here such code
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using Microsoft.CSharp;

namespace ConsoleCompiler
{
 internal class Program
{
 private static void Main(string[] args)
{
 Console.WriteLine("Enter file name:");
 string name = Console.ReadLine();
 string content = Console.ReadLine();

 string source =
 @" namespace Prog
{ 
 public class Bar 
 { 
 static void Main(string[] args) 
 { 
 Bar.SayHello(); 
System.Console.WriteLine(content);
System.Console.ReadKey();
 } 
 } 
} 
";
 Dictionary<string, string> providerOptions = new Dictionary<string, string> 
 { 
 {"CompilerVersion", "v4.0"} 
};
 CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);

 CompilerParameters compilerParams = new CompilerParameters { OutputAssembly = name + ".exe", GenerateExecutable = true };
 CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, source);

 Console.WriteLine("Number of Errors: {0}", results.Errors.Count);
 foreach (CompilerError err in results.Errors)
{
 Console.WriteLine("ERROR {0}", err.ErrorText);
}
Console.ReadLine();
}
}
}</string></string>


The question of how peremennoy string content display in the code which should skompilirovana, namely to string source ?
Thank you
June 10th 19 at 14:38
1 answer
June 10th 19 at 14:40
Replace the content in the source on its value.
what if the value of the content need to enter from the main code, and then this value is assigned in the code you want to compile? How to implement it? - carmel15 commented on June 10th 19 at 14:43
, replace the plot line with the name of the variable to the entered from the main code value. - Alize_Moen commented on June 10th 19 at 14:46
or I something not that doing, or how, but it does not work, gives an error me that validation data not found in context. - carmel15 commented on June 10th 19 at 14:49
all) problems is not present, used .Replace
Thanks for the help. - carmel15 commented on June 10th 19 at 14:52

Find more questions by tags C#