简体   繁体   中英

How to convert a object of one class to another (where the class members are different)

I have a scenario wherein I have an (objA) of (Class A) and (objB) of (Class B). The member variables in Class A and Class B do not match. I need to assign data from objA to objB. I searched around and found solutions which are related to mapping objA into objB where member variables are the same. But I havent found anything when members in Class A and class B are different.

Below are 2 sample classes (class A and class B) to give an idea.

public class Class A
{
    private ABCHeaderType ABCHeaderField;
    private string title;
    private string date;
    private string id;
    private string location;
    private string status;
    private string hashNumber;      
    .....skipped getter setters     
}   
public class ABCHeaderType { 
    private string version;    
    private string outputTypeField;    
    private int langCd;       
    private string dateTime;    
    private string hashField;
        .....skipped getter setters
}

public class Class B
{
    public Input input { get; set; }

}
public class Input
{
    public Output output{ get; set; }
}
public class Output
{
    public string Title { get; set; }
    public string Date { get; set; }
    public string id { get; set; }
    public string location { get; set; }
    public string Status { get; set; }
    public string laterDate { get; set; }
    public string hashNumber { get; set; }
    public Info info { get; set; }
}
public class Info
{
    public string name { get; set; }
    public string ht { get; set; }
    public string type { get; set; }
    public string res { get; set; }
    public string wd { get; set; }
    public string ornt { get; set; }
}

Right now I am copying objA into objB using a converter class and copying over using the code snippet below

objB.input.output.Title = objA.Title;
objB.input.output.Date = objA.Date;
objB.input.output.id = objA.id;
objB.input.output.location = objA.location;
objB.input.output.Status = objA.Status;
objB.input.output.hashNumber = objA.hashNumber;

objB.input.output.inf.type = objA.ABCHeaderField.outputTypeField

Thanks...

You can create an interface BaseObject with base fields that can be implemented by classes A and Output:

public interface BaseObject
{
    string Title { get; set; }
    string Date { get; set; }
    string id { get; set; }
    string location { get; set; }
    string Status { get; set; }
    string laterDate { get; set; }
    string hashNumber { get; set; }
    Info info { get; set; }
}
public class A : BaseObject
{
    public string Title { get; set; }
    public string Date { get; set; }
    public string id { get; set; }
    public string location { get; set; }
    public string Status { get; set; }
    public string laterDate { get; set; }
    public string hashNumber { get; set; }
    public Info info { get; set; }
  // your code....
}
public class B
{
    public Input Input { get; set; }
}
public class Input
{
    public BaseObject Output{ get; set; }
}

public class Output : BaseObject
{
    public string Title { get; set; }
    public string Date { get; set; }
    public string id { get; set; }
    public string location { get; set; }
    public string Status { get; set; }
    public string laterDate { get; set; }
    public string hashNumber { get; set; }
    public Info info { get; set; }
}

And then you can do it:

var objB = new B
{
     Input = new Input
     {
        Output = new A()
      }
};

If obejct was initialized and was initialized field Input:

objB.Input.Output = objA;

From the comments:

The 2 classes are related in a way that - consider objA is an incoming object from one system and objB is an object to send out to a different other system. But objB needs the data from objA

How about a class "shared Data"?. Something that has all the fields both sides got in common. Depending on the case, a readonly struct might be better - otherwise you may have to clone.

public readonly struct SharedData{
    public readonly string Title;
    public readonly string Date;
    public readonly string id;
    public readonly string location;
    public readonly string Status;
    public readonly string laterDate;
    public readonly string hashNumber;
}

The MVVM pattern deals a lot with presentation. Indeed that is most of what the View Model - half those letters - are there for. In MVVM you need properties with change notification, often ones that accept raw strings too. The view usually does not have those - and can not be modified to get those.

"If you can not modify it, wrap it into something you can modify". Most View classes can not be modified or inherited. Because they come from some outside source, or their current behavior is needed. So you usually have to write ViewModel classes for the primary purpose of wrapping around those View classes. (There are other advantages to making VM classes, but those tend to be MVVM/XAML specific).

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