0

I was wondering if there is any way to pass a variable value in a code that will be compiled One Time using CSharpCodeProvider .

for example :

string code = @"
using System;

namespace First
{
    public class Program
    {
       public int Value; // pass this value
        public static void Main()
        {
        " +
            "Console.WriteLine(\"Hello + Value\");"
            + @"
        }
    }
}

";

Compile Method :

public void Compile(String Code)
{
    CSharpCodeProvider provider = new CSharpCodeProvider();
    CompilerParameters parameters = new CompilerParameters();

    parameters.ReferencedAssemblies.Add("System.Drawing.dll");
    parameters.GenerateInMemory = true;
    parameters.GenerateExecutable = false;

    CompilerResults results = provider.CompileAssemblyFromSource(parameters, Code);
}

So i want to be able to pass a value of the Value example 2

and what i meant by ONE TIME is like compile time so the compiled code will always in its run-time will display the value : 2 whenever i executed the application .

I hope its clear !

svick
  • 236,525
  • 50
  • 385
  • 514
Left panda
  • 148
  • 2
  • 15
  • Can you make sure the `code` is in a specific format? E.g. that the value to set is `public int Value = {0};`? – svick Oct 06 '16 at 23:22

1 Answers1

0

Solved Using Mono.Cecil reference details Documentatin

Left panda
  • 148
  • 2
  • 15