开发者

How do I serialize a generated class?

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 er开发者_运维知识库ror.

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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜