是否可以使用Roslyn创建一个Portable类库?

Pau*_*ilá 6 c# portable-class-library roslyn

这是一个非常简单的代码,它生成一个可以从Portable类库中引用的dll,但是,它很容易出错,因为当我添加任何引用时它接受非可移植引用.我怎样才能确定我正在尝试生成的内容在Portable配置文件上?,这是代码:

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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Imm*_*rth 5

是.可移植类库(PCL)作为一个概念对编译器是透明的.它基本上只是一个项目系统和参考组件功能.如果要创建一个可定位的可移植类库,例如.NET for Windows Store应用程序和.NET 4.5,则应该针对此文件夹中的程序集进行编译:

%ProgramFiles(x86)%\Reference Assemblies\Microsoft\Framework\.NETPortable\v4.5\Profile\Profile7
Run Code Online (Sandbox Code Playgroud)

每个配置文件文件夹都有一个名为SupportedFrameworks的子目录,它指示它支持哪些框架.

要使PCL在Visual Studio中运行良好,您还应该包含TargetFrameworkAttribute.确保正确设置版本和配置文件.对于上面的例子,你需要

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

我认为我们不会在Visual Studio之外发布这些程序集,因此您需要安装Visual Studio 2010(安装PCL扩展程序)或Visual Studio 2012.