Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

c# - How to map properties of two different objects?

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; }
}

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.

Note: It can't have any prefix for manager. It can be anything. (Ex: mgrId can be like mgrCode)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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));
}

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));
        });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...