2017-06-09 46 views
0

我想在.NET Lambda中使用.NET Framework 4.7解决方案和.NET Core解决方案之间共享代码。.NET Framework 4.7中的.NET标准类库项目

我已经创建了一个.NET Standard 1.6类库项目,并已将一些代码从.NET Framework 4.7解决方案移到此项目中以便共享它。

除了一件事情之外,所有工作都很好 - 代码是由.NET解决方案中的BinaryFormatter序列化的DTO类。

例如:

[Serializable] 
public class BillableOptionalOperationDto 
{ 
    public string OperationDescription { get; set; } 
    public string Note { get; set; } 
    public decimal UnitPriceIncGst { get; set; } 
} 

我创建了一个填充工具使代码在.NET标准项目编译。

namespace System 
{ 
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Delegate)] 
    public class SerializableAttribute : Attribute 
    { 
    } 
} 

.NET标准项目作为Nuget包发布到TeamCity中的Nuget服务器。

在.NET Framework解决方案,我(非常正确)收到此错误:

Error CS0433 The type 'SerializableAttribute' exists in both 'AutoGuru.Shared.Quoting, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

有没有在.NET 1.6标准类库使用的BinaryFormatter一个类的方法吗?

回答

2

您将无法使用使用.NET的核心运行时的BinaryFormatter序列化,但你至少可以交叉编译使用这个NuGet包

https://www.nuget.org/packages/System.Runtime.Serialization.Formatters/

不过,请注意组件,.NET标准结合是一团糟,没有大量的程序集重定向,你的代码会很好地编译,但是在运行时抛出程序集没有发现异常。据说.NET标准2.0将解决这个问题,但我并没有屏住呼吸。

相关问题