Can ReSharper generate code that copies properties from one object to another?

It’s really easy. ReSharper doesn’t do it, but you can use a super duper REGEX!

In Visual Studio:

    public string Email { get; set; }
    public string CellPhone { get; set; }
    public int NumChildren { get; set; }
    public DateTime BirthDate { get; set; }
  1. Select all your properties. Hit CTRL-D to copy down.

  2. Now hit CTRL-H to replace. Make sure .* is selected for Regex matching.

  3. Replace: public [\w?]* (\w*) .* (This Regex may need to be tweaked)

  4. With: dest.$1 = source.$1;

Now you have some beautiful code you can put in a method of your choosing:

    dest.Email = source.Email;
    dest.CellPhone = source.CellPhone;
    dest.NumChildren = source.NumChildren;
    dest.BirthDate = source.BirthDate;

EDIT: New alternatives

  1. You can use AutoMapper for dynamic runtime mapping.
  2. Mapping Generator is really nice for static mapping. It can generate the code above and it works well with R#.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)