Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This is a very simple code that generates a dll that can be referenced from a Portable class library, but, it is prone error because when I add any reference it accepts non portable references. How can I tell for sure that what I'm trying to generate is on 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);
            }
        }
    }
}
like image 497
Paulo Vilá Avatar asked Apr 27 '13 11:04

Paulo Vilá


People also ask

What is portable class library?

The Portable Class Library project enables you to write and build managed assemblies that work on more than one . NET Framework platform. You can create classes that contain code you wish to share across many projects, such as shared business logic, and then reference those classes from different types of projects.

What is Roslyn in .NET core?

Roslyn, the . NET Compiler Platform, empowers the C# compiler on . NET Core and allows developers to leverage the rich code analysis APIs to perform code generation, analysis and compilation.

What is PCL project?

PCL projects target specific profiles that support a known set of BCL classes/features. However, the down side to PCL is that they often require extra architectural effort to separate profile specific code into their own libraries.


1 Answers

Yes. Portable Class Libraries (PCL) as a concept is transparent to the compiler. It's basically a project system and reference assemblies feature only. If you want to create a portable class library that targets, say .NET for Windows Store apps and .NET 4.5, you should compile against the assemblies in this folder:

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

Every profile folder has a subdirectory called SupportedFrameworks which indicates which frameworks it supports.

To make the PCL work great in Visual Studio, you should also include the TargetFrameworkAttribute. Make sure version and profile are set correctly. For the example above you would need

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

I don't think we ship these assemblies outside of Visual Studio, so you will need an installation of Visual Studio 2010 (with the PCL extension installed) or Visual Studio 2012.

like image 70
Immo Landwerth Avatar answered Oct 03 '22 17:10

Immo Landwerth