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

后端 未结 5 759
灰色年华
灰色年华 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:44

    Write a implicit operator in anyone class

        public static implicit operator StudentDTO(Student student)
        {
    
            //use skeet's library
    
            return PropertyCopy.CopyFrom(student);
    
        }
    

    now you can do that

    StudentDTO studentDTO = student;
    

提交回复
热议问题