.NET便携式类库[可序列化]属性 [英] .NET Portable Class Library [Serializable] Attribute

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

问题描述

我有一个通用的类库,其中包含用于服务器和客户端的许多模型.由于客户端在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远程处理/封送处理一起使用,并使其驻留在可移植类库中?

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支持库,其中包含"mock" [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 的.NET方法的引用. >,使用所谓的诱饵和开关" 技术. .NET模拟使用[Serializable]的调用转发到 mscorlib 中的.NET实现. .compilerservices.typeforwardedtoattribute.aspx"rel =" nofollow noreferrer> [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类似物.核心组件.我已经在 StackOverflow答案中更详细地概述了这种方法.

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便携式类库[可序列化]属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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