c# - Programmatically compiling source code using Roslyn -
so i've been trying compile piece of code programmatically roslyn. reasons references add don't end in compilation class. when @ referenced assembly after use 'addreferences', list empty. hence, when try emit, "object" not defined in diagnostics. can point me problem?
microsoft.codeanalysis.syntaxtree syntaxtree = csharpsyntaxtree.parsetext(@" public static class program { public static void main() { system.console.writeline(""hello""); } } "); string autoreferences = @"mscorlib.dll,system.core.dll"; list<string> usings = new list<string>(); string netassembliesdirectory = path.getdirectoryname(typeof(object).assembly.location); var refs = new list<string>(); foreach (string reference in autoreferences.split(',')) refs.add(netassembliesdirectory + "\\" + reference); csharpcompilation compilation = csharpcompilation.create("consoletest") .withoptions( new csharpcompilationoptions(outputkind.consoleapplication).withusings("system")) .addsyntaxtrees(syntaxtree); compilation.addreferences(refs.where(r => r != "").select(r => metadatareference.createfromfile(r))); var er = compilation.emit(@"c:\" + "consoletest");
roslyn objects immutable.
compilation.addreferences()
returns new compilation instance references.
you're ignoring new instance.
you need call emit()
on compilation instance has references.
Comments
Post a Comment