c# - Roslyn - CodeDom: HowTo dynamically compile Code to Universal-Windows-Library -


i generating .net dll dynamically containing wrapper classes wpf project. doing system.codedom.compiler.codedomprovider class.

now have create wrapper class universal-windows-dll. since system.codedom.compiler.codedomprovider class still uses old .net compiler had switch new roslyn compiler (by adding nuget package microsoft.codedom.providers.dotnetcompilerplatform). replaced instantiation of code-dom-provider new csharpcodeprovider.

new microsoft.codedom.providers.dotnetcompilerplatform.csharpcodeprovider(); 

the code compiles fine found no way set targetframework / compilerversion. old codedomprovider had compileroptions able specify compilerversion ect. new roslyn not have option (or stupid find it).

the result compiles dll normal .net 4.x dll. need universal-windows dll since used in universal-project.

browsing internet found many different ways use roslyn compiler. of them seem old beta-versions of compiler, therefore none of them works. roslyn.compilers namespace (which used in examples) seems namespace betas.

does know how use roslyn compiler properly? don't want modify compiler. want generate dll dynamically compiling sourcecode, have specify platform target.

there's option reference compiler , runtime version. latest release of roslyn have new feature can specify target framework want use , version of compiler want use.

i looking around explore latest roslyn library compile csharp6 version program compile against 4.6 framework. below working sample.

note, runtimepath variable pointing .net framework libraries , csharpparseoptions.default.withlanguageversion(languageversion.csharp6) option in parser.

 public class program     {         private static readonly ienumerable<string> defaultnamespaces =             new[]             {                 "system",                  "system.io",                  "system.net",                  "system.linq",                  "system.text",                  "system.text.regularexpressions",                  "system.collections.generic"             };          private static string runtimepath = @"c:\program files (x86)\reference assemblies\microsoft\framework\.netframework\v4.6\{0}.dll";          private static readonly ienumerable<metadatareference> defaultreferences =             new[]             {                 metadatareference.createfromfile(string.format(runtimepath, "mscorlib")),                 metadatareference.createfromfile(string.format(runtimepath, "system")),                 metadatareference.createfromfile(string.format(runtimepath, "system.core"))             };          private static readonly csharpcompilationoptions defaultcompilationoptions =             new csharpcompilationoptions(outputkind.windowsruntimeapplication)                     .withoverflowchecks(true)                     .withoptimizationlevel(optimizationlevel.release)                     .withusings(defaultnamespaces);          public static syntaxtree parse(string text, string filename = "", csharpparseoptions options = null)         {             var stringtext = sourcetext.from(text, encoding.utf8);             return syntaxfactory.parsesyntaxtree(stringtext, options, filename);         }          public static void main(string[] args)         {             //referencefinder finder = new referencefinder();             //finder.find("read");              var filetocompile = @"c:\users\..\documents\visual studio 2013\projects\signalr_everything\program.cs";             var source = file.readalltext(filetocompile);             var parsedsyntaxtree = parse(source, "", csharpparseoptions.default.withlanguageversion(languageversion.csharp6));              var compilation                 = csharpcompilation.create("test.dll", new syntaxtree[] { parsedsyntaxtree }, defaultreferences, defaultcompilationoptions);             try             {                 var result = compilation.emit(@"c:\temp\test.dll");                  console.writeline(result.success ? "sucess!!" : "failed");             }             catch (exception ex)             {                 console.writeline(ex);             }             console.read();         }     } 

Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -