.NET 可移植类库 [Serializable] 属性 [英] .NET Portable Class Library [Serializable] Attribute

查看:21
本文介绍了.NET 可移植类库 [Serializable] 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个公共类库,其中包含许多用于我的服务器和客户端的模型.由于客户端在 Xamarin 下运行,因此公共库必须是可移植类库 (PCL).

I have a common class library containing many models for my server and my client. Since the client is running under Xamarin, the common library must be a Portable Class Library (PCL).

在我的服务器中,这些对象通过 AppDomain Remoting/Marshaling 传递,所以据我所知,对象要么需要标记为 [Serializable] 或从 MarshalByRefObject 继承>

In my server, these objects are passed around with AppDomain Remoting/Marshaling, so to my understanding an object either needs to be marked as [Serializable] or inherit from MarshalByRefObject

在 PCL 中,我无法对我的任何模型执行这些操作.

Being in a PCL, I cannot do either of these things to any of my models.

我的问题是:如何使这些对象与 AppDomain Remoting/Marshaling 一起使用,并让它们驻留在可移植类库中?

My question is: How can I make these objects work with AppDomain Remoting/Marshaling and let them reside in a Portable Class Library?

推荐答案

我创建了一个名为 CSShim 的 PCL 支持库,其中包含一个模拟"[Serializable] 属性.如果此库是从您的 PCL 库中引用的,您可以在代码中使用 [Serializable].

I have created a PCL support library called CSShim that contains a "mock" [Serializable] attribute. If this library is referenced from your PCL library, you can use [Serializable] in your code.

然后,当您在常规 .NET 桌面应用程序中使用您的 PCL 库时,对 PCL CSShim 的引用被替换为对 CSShim,使用所谓的"bait-and-switch" 技术..NET 模拟将 [Serializable] 的调用转发到 mscorlib 中的 .NET 实现,使用 [TypeForwardedTo].

Then, when you consume your PCL library in a regular .NET desktop application, the reference to the PCL CSShim is replaced with a reference to the .NET anolugue of CSShim, using the so-called "bait-and-switch" technique. The .NET analogue forwards the invocation of [Serializable] to the .NET implementation in mscorlib using [TypeForwardedTo].

CSShim 目前可从 NuGet 获得,用于 PCL 配置文件 259,面向 .NET Framework 4.5 及更高版本、Windows 8 及更高版本、Windows Phone 8.1、Windows Phone Silverlight 8 及更高版本、Xamarin Android 和 Xamarin iOS.

CSShim is currently available from NuGet for PCL profile 259, targeting .NET Framework 4.5 and higher, Windows 8 and higher, Windows Phone 8.1, Windows Phone Silverlight 8 and higher, Xamarin Android and Xamarin iOS.

CSShim 源代码可在 Github 上获得.如果 PCL 库仅针对 .NET 4.5 及更高版本是一个限制,理论上您可以将 PCL 库重新定位到 .NET Framework 4 配置文件,例如配置文件 328,尽管重新定位可能是艰难的旅程":-)

The CSShim source code is available on Github. If it is a limitation that the PCL library only targets .NET 4.5 and higher, you could theoretically re-target the PCL library to a .NET Framework 4 profile such as profile 328, although re-targeting may be "a rough ride" :-)

或者,您可以创建自己的 PCL 支持库,仅包含与 SerializableAttribute 相关的类型的模拟"实现,并使用类型转发来创建支持库的 .NET 模拟以调用有效的.NET 核心程序集中的类型.我在 this StackOverflow answer 中更详细地概述了这种方法.

Alternatively, you could create your own PCL support library containing only "mock" implementations of the types related to SerializableAttribute, and create a .NET analogue of the support library using type forwarding to invoke the valid types in the .NET core assemblies. I have outlined this approach in more detail in this StackOverflow answer.

这篇关于.NET 可移植类库 [Serializable] 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆