我的问题是,以最可维护的方式将一个对象映射到另一个对象的最佳方法是什么。我无法更改我们获得的 Dto 对象的设置方式以更加规范化,因此我需要创建一种方法将其映射到我们的对象实现。
这是显示我需要发生的事情的示例代码:
class Program
{
    static void Main(string[] args)
    {
        var dto = new Dto();
        dto.Items = new object[] { 1.00m, true, "Three" };
        dto.ItemsNames = new[] { "One", "Two", "Three" };            
        var model = GetModel(dto);
        Console.WriteLine("One: {0}", model.One);
        Console.WriteLine("Two: {0}", model.Two);
        Console.WriteLine("Three: {0}", model.Three);
        Console.ReadLine();
    }
    private static Model GetModel(Dto dto)
    {
        var result = new Model();
        result.One = Convert.ToDecimal(dto.Items[Array.IndexOf(dto.ItemsNames, "One")]);
        result.Two = Convert.ToBoolean(dto.Items[Array.IndexOf(dto.ItemsNames, "Two")]);
        result.Three = dto.Items[Array.IndexOf(dto.ItemsNames, "Three")].ToString();
        return result;
    }
}
class Dto
{
    public object[] Items { get; set; }
    public string[] ItemsNames { get; set; }
}
class Model
{
    public decimal One { get; set; }
    public bool Two { get; set; }
    public string Three { get; set; }
}
我认为如果我有某种映射器类可以接收模型对象 propertyInfo、我想要转换的类型以及我想要提取的“项目名称”,那将会很棒。有没有人有任何建议让这个更清洁?
谢谢!
