如何序列化生成的类?

发布于 2024-10-20 04:52:12 字数 702 浏览 1 评论 0原文

我正在开发一个项目,我想通过 Web 服务从数据库发送一个实体。

我将此作为我的数据合同。

[DataContract]
public class CreateAlumniRequest
{
    [DataMember]
    public List<Alum.Data.EmploymentHistory> lstEmploymentHistory;
}

但是,我不断收到此错误。

类型 '明矾.Data.Base.EmploymentHistoryBase' 无法序列化。考虑标记 它与 DataContractAttribute 属性,并标记其所有 您想要序列化的成员 DataMemberAttribute 属性。如果 type 是一个集合,考虑标记 它与 CollectionDataContractAttribute。

这告诉我,我可能需要去EmploymentHistory类并将其标记为DataContract。

问题是就业历史是由内部 ORM 工具生成的 - 我无法真正对其进行任何更改。

是否可以序列化EmploymentHistory而不在类中添加该属性?

还有其他解决办法吗?我唯一能想到的就是在可序列化的类中重新创建EmploymentHistory并手动映射对象,这听起来像是大量重复。

I am working on a project and I would like to send an entity from the database through a web service.

I have this as my data contract.

[DataContract]
public class CreateAlumniRequest
{
    [DataMember]
    public List<Alum.Data.EmploymentHistory> lstEmploymentHistory;
}

However, I keep getting this error.

Type
'Alum.Data.Base.EmploymentHistoryBase'
cannot be serialized. Consider marking
it with the DataContractAttribute
attribute, and marking all of its
members you want serialized with the
DataMemberAttribute attribute. If the
type is a collection, consider marking
it with the
CollectionDataContractAttribute.

Which tells me I probably need to go to the EmploymentHistory class and mark it as a DataContract.

The problem is EmploymentHistory is generated by an inhouse ORM tool - I can't really make any changes to it.

Is it possible to make serialize EmploymentHistory without adding that attribute in the class?

Is there any other solution? The only thing I can think of doing is re-creating EmploymentHistory in a serializable class and manually mapping the objects, which sounds like a lot of duplication.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心不设防 2024-10-27 04:52:12

我不确定您要使用哪个序列化程序,但如果 Alum.Data.EmploymentHistory 无法序列化,因为它没有标记为可序列化或由于其他原因......应该能够右键单击它并提取接口。使用重构菜单创建一个类来实现接口,并在成员上实现 {get;set;}。然后你可以使用 automapper 来映射数据。

听起来很多,但实际上会非常快......并且会避免所有手动映射。即使您不使用自动映射器,它也绝对值得一看。在很多情况下,您需要将数据从一个对象复制到另一个对象......“AssertConfigurationIsValid”使测试您的映射变得容易。如果你的数组或列表或任何类型的嵌套对象,事情都会自动处理......非常酷......检查一下。

I'm not sure which serializer you are trying to use but if Alum.Data.EmploymentHistory can't be serialized because it isn't marked as serializable or for some other reason... should be able to just right click on it and extract the interface. Using create a class to implement the interface using the refactoring menu and have just implement {get;set;} on the members. Then you could use automapper to map the data over.

It sound like alot but it actually would be really quick... and would avoid all the manual mapping. Even if you don't use automapper for this it is definitely worth looking at. There are so many situations where you need to copy data from one object to another... the "AssertConfigurationIsValid" makes testing your mapping easy. If you arrays or lists or any kind of nested objects things are handled automagically ... very cool... check it out.

失退 2024-10-27 04:52:12

如果您的代码生成器创建了部分类,您可以将缺少的属性添加到新的部分文件中。

如果没有您可以尝试不同的开源序列化程序,例如 SharpSerializerprotobuf-net

If your codegenerator creates partial classes you can add the missing attribute to new partial files.

If not you can try a different open source serialiser like SharpSerializer or protobuf-net.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文