Cast an object into an object from a derived class

前端 未结 4 1512
野的像风
野的像风 2021-01-25 06:15

I have a class Record that works fine:

public class Record 
{
    protected string table;
    protected string idcolumn;
    public Record(string _t         


        
4条回答
  •  野性不改
    2021-01-25 07:12

    You'll have to convert into a new object, rather than cast it. To do this without writing the mapping code one property at a time, the easiest way would be to serialize the Record object, then deserialize it as an Order. You can use whatever serialization technique you like (XmlSerializer, JavaScriptSerializer, JSON.NET, etc.).

    This assumes that all the data is accessible in public properties with plain getters/setters (which you don't in your example, but you should do). If you don't want to do that, you can use reflection to loop through the fields of Record, getting the values from there, and using reflection to populate the fields of Order.

提交回复
热议问题