How to copy value from class X to class Y with the same property name in c#?

后端 未结 5 763
灰色年华
灰色年华 2020-11-28 08:49

Suppose I have two classes:

public class Student
{
    public int Id {get; set;}
    public string Name {get; set;}
    public IList Courses{ g         


        
5条回答
  •  时光说笑
    2020-11-28 09:26

    FYI

    When I was having the same question I found AutoMapper (http://automapper.codeplex.com/) Then after reading AboutDev's answer i was done some simple test, the results pretty impressive

    here the test results:

    Test Auto Mapper:22322 ms

    Test Implicit Operator:310 ms

    Test Property Copy:250 ms

    Test Emit Mapper:281 ms

    And i would like to underline that it is sample only with classes(StudentDTO, Student) which have only a couple of properties, but what would happened if classes would have 50 - 100 properties, i guess it will affect performance dramatically.

    More tests details here: Object copy approaches in .net: Auto Mapper, Emit Mapper, Implicit Operation, Property Copy

提交回复
热议问题