Is it possible to create a portable class library with Roslyn?

This is very simple code that generates a referenced DLL from a portable class library, but it is error prone because when I add a link, it accepts non-portable links. How can I say for sure that what I'm trying to create is in the Portable profile ?, Here is the code:

using System.IO; using Roslyn.Compilers; using Roslyn.Compilers.CSharp; namespace Ros1 { class Program { static void Main(string[] args) { SyntaxTree tree = SyntaxTree.ParseText( @"using System; namespace HelloWorld { public class A { public int Sum(int a, int b) { return a + b; } } }"); var co = new CompilationOptions(OutputKind.DynamicallyLinkedLibrary); var compilation = Compilation.Create("HelloWorld", co) .AddReferences(MetadataReference.CreateAssemblyReference("mscorlib")) .AddSyntaxTrees(tree); using (var file = new FileStream("Sum.dll", FileMode.Create)) { compilation.Emit(file); } } } } 
+6
source share
2 answers

Yes. Portable Class Libraries (PCL) as a concept is transparent to the compiler. This is mainly a function of the design system and reference assemblies. If you want to create a portable class library that is targeted at, say, .NET for Windows Store and .NET 4.5 applications, you must compile the assembly in this folder:

 %ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.5\Profile\Profile7 

Each profile folder has a subdirectory called SupportedFrameworks, which indicates which framework it supports.

To make PCL work fine in Visual Studio, you must also enable TargetFrameworkAttribute . Make sure the version and profile are installed correctly. For the example above you will need

 [assembly: TargetFramework(".NETPortable,Version=v4.5,Profile=Profile7", FrameworkDisplayName=".NET Portable Subset")] 

I do not think that we are sending these assemblies outside of Visual Studio, so you will need to install Visual Studio 2010 (with the PCL extension installed) or Visual Studio 2012.

+5
source

I believe Paulo asks if Roslyn can be used in a Silverlight / Portable application. The answer is no, Roslyn currently only works with complete confidence in the desktop CLR. This, of course, is what we would like to include in the future.

0
source

Source: https://habr.com/ru/post/943770/


All Articles