1

I have a C# code generated dynamically using System.CodeDom.CodeCompileUnit in runtime, I compile it using Microsoft.CSharp.CSharpCodeProvider , What I want is to insert an already defined & initialized Variable (an ArrayList) to this string so I could use it before compile, How should I do this?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Vahid2023
  • 863
  • 3
  • 11
  • 33

1 Answers1

2

You need to generate the code which creates this variable and fills it with the values you have. Or you can add an argument for one of the methods you generate and pass it at runtime. There's no magic method which will do it fo you.

CodeDom is incomplete, obsolete and outdated. Nowadays you should use T4 (if you want code as text) or Expressions and IL (if you want to execute dynamically generated code). If you use Expessions, inserting a variable is trivial. With T4, it'll take just a couple of lines of code, but will require doing it manually too.

Athari
  • 33,702
  • 16
  • 105
  • 146