简体   繁体   中英

How to clone an unserializable object? (C#)

I am working with Kinect for Windows version 2 and meet a problem. I try to serialize the Body object and send it through the Internet. However Body object is not sterilisable. Although I can extract some key information from a Body object and create my own object, I may lose some information. My question is how to clone all information from a Body object to my own serializable object? Thank you.

It's alright. Just do the following logic:

  1. Use reflection to loop through the properties of the object you wanna clone.
  2. You can setup the data with your predefined custom class. (Perhaps you might want to generate the XML schema according to the object's properties and from there you create your own predefined custom class).

Hope this concept helps. If not, let's discuss further.

If the purpose of serialization is to reconstruct it at the other end, the first thing you need to determine whether the constructor and setters exist for you to create an equivalent on the other side. If it is purely for an independent representation that your server side needs to interact with, you have a much simpler task.

My recommendation would be to inspect the body object both via the public interface available through documentation and via reflection in the debugger to determine what data you can and want to extract and build a custom, serializable class based on that hierarchical model.

If all the data you need to extract is publicly accessible, simply writer a builder class that takes the body object as its input and constructs your custom class as the output. If it's not publicly accessible, you may need to use reflection to explore the pieces you need. I would advise the reflection code to be manually built as to avoid cycles in the object graph that may exist in a private class as this.

If cloning is what you're concerned with, use AutoMapper .

First you'll need to install AutoMapper using NuGet...

PM> Install-Package AutoMapper

Then check out this example and adapt it to your own needs...

void Main()
{
    AutoMapper.Mapper.CreateMap<User, MyUser>()
        .ForMember(myUsers => myUsers.Name, users => users.MapFrom(property => string.Format("{0} {1}",property.FirstName, property.LastName)));

    User user = new User
    {
        FirstName = "James",
        LastName = "Doe",
        DateOfBirth = DateTime.UtcNow
    };


    MyUser myUser = AutoMapper.Mapper.Map<MyUser>(user);
}

public class MyUser
{
    public string Id { get; set; }
    public string Name { get; set; }
    public DateTime DateOfBirth { get; set; }

}

public class User 
{
    public User()
    {
        this.Id = Guid.NewGuid().ToString();
    }
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
}

In the example above, AutoMapper figures out that it can map the Id property of MyUser and User class because they're named identically, however we needed to create a custom map to map User.FirstName and User.LastName to MyUser.Name property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM