如何映射两个不同对象的属性? [英] How to map properties of two different objects?

查看:142
本文介绍了如何映射两个不同对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何映射两个不同对象的字段,将值赋给它

I want to know how to map fields of two different objects and assign the values to it.

Eample:

public class employee
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class manager
{
    public int MgrId { get; set; }
    public string MgrName { get; set; }
}

现在我有一个List对象。我想要的值分配给经理级。任何自动的方式来做到这一点。我能做到这一点,并明确地给它赋值。但我的目标是,多数民众赞成这个问题非常巨大的。我不想使用任何第三方工具太

Now I have a List object. I want to assign the values to "manager" class. Any automatic way to do that. I can do it explicitly and assigning values to it. But my object is very huge thats the problem. I dont want to use any third party tools too.

注意:它不能有任何经理前缀。它可以是任何东西。 (例如:MGRID可以像mgrCode)

推荐答案

您可以使用反射它,甚至忽略了物业外壳(注意 employee.ID manager.MgrId ):

You could use reflection for it, even by ignoring the property casing (notice the employee.ID vs. manager.MgrId):

class Program
{
    static void Main(string[] args)
    {
        var employee = new Employee() { ID = 1, Name = "John" };
        var manager = new Manager();
        foreach (PropertyInfo propertyInfo in typeof(Employee).GetProperties())
        {
            typeof(Manager)
                .GetProperty("Mgr" + propertyInfo.Name,
                    BindingFlags.IgnoreCase |
                    BindingFlags.Instance |
                    BindingFlags.Public)
                .SetValue(manager,
                    propertyInfo.GetValue(employee));
        }
    }
}

public class Employee
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class Manager
{
    public int MgrId { get; set; }
    public string MgrName { get; set; }
}

如果您不知道经理<

If you don't know the Mgr prefix, you could only match by suffixes:

foreach (PropertyInfo propertyInfo in typeof(Employee).GetProperties())
{
    typeof(Manager).GetMembers()
        .OfType<PropertyInfo>()
        .FirstOrDefault(p => p.Name.EndsWith(propertyInfo.Name, 
            StringComparison.CurrentCultureIgnoreCase))
        .SetValue(manager,
            propertyInfo.GetValue(employee));
}



一个很窄的和不切实际的假设:映射基于属性的订单上(如果所期望的2种类型具有相同的顺序和数量,作为属性名的唯一区别定义的属性)。我不建议在现实生活中使用它的人,不过,在这里它是(只是使它更加的脆弱的:)):

And a very narrow and impractical assumption: mapping based on the property order (if you are expecting the 2 types to have properties defined in the same sequence and number, the only difference being the property names). I wouldn't recommend anyone using it in real life, but still, here it is (just to make it more fragile :) ):

typeof(Employee)
    .GetProperties()
    .Select((p, index) =>
        new { Index = index, PropertyInfo = p })
    .ToList()
    .ForEach(p =>
        {
            typeof(Manager)
                .GetProperties()
                .Skip(p.Index)
                .FirstOrDefault()
                .SetValue(manager,
                    p.PropertyInfo.GetValue(employee));
        });

这篇关于如何映射两个不同对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆