0

I ask with example, Lets say I have the following code.

fullcommand = @"public class oldTest
{
    public static void oldTestMethod(){
        Console.WriteLine(""oldTest Class"");
    }
}"

var syntaxTree = CSharpSyntaxTree.ParseText(fullCommand);
var compilation = CSharpCompilation.Create(
                assemblyName,
                new[] {syntaxTree},
                references,
                options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary,allowUnsafe:true));

var ms = new MemoryStream();
var result = compilation.Emit(ms);

And I will compile the above code with Roslyn in memory. next i want to compile another code in memory to use the above compiled class, lets say the below.

new_fullcommand = @"public class newTest
    {
        public static void newTest(){
            oldTest.oldTestMethod();
        }
    }"

    var syntaxTree = CSharpSyntaxTree.ParseText(new_fullcommand);
    var compilation = CSharpCompilation.Create(
                    assemblyName,
                    new[] {syntaxTree},
                    references,
                    options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary,allowUnsafe:true));

    var ms = new MemoryStream();
    var result = compilation.Emit(ms);

how can i make the second code to use the first code as its reference? or use it?

Emily Wong
  • 297
  • 3
  • 10

1 Answers1

0

The easiest way would probably be to pass in multiple syntaxTree objects when you create your compilation.

However, if you want to build up your compilation incrementally I believe you can use Compilation.AddSyntaxTrees to your first compilation object.

JoshVarty
  • 9,066
  • 4
  • 52
  • 80
  • I cant do that, I need to compile SyntaxTrees separately because i am not aware of upcoming codes. – Emily Wong Mar 18 '19 at 01:56
  • I found this pretty much relevant, but i cant get it work. https://stackoverflow.com/questions/38646619/how-to-use-a-roslyn-scripting-submission-as-an-assembly-in-other-roslyn-compilat Any idea how to fix it? – Emily Wong Mar 18 '19 at 01:57