Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm looking for a way to accelerate a repeatable task when I write code. I have ReSharper and I'm thinking a customization could do what I need.

I have two objects of the same type. I want to copy all of the public properties of one object to the other object. I want the tool, ReSharper in this case, to do generate the code for me. I'll tell it the names of the first object and the second object. I want it to find all the public properties of the first object and copy the values to the second object.

Here's the type of code I'm looking to have generated with a tool like ReSharper:

foo.Name = moo.Name;
foo.Age = moo.Age;
foo.City = moo.City;

Automating this simple code that copies values from right to left would save a ton of time and I'm thinking that ReSharper can do it. However, I haven't seen anything pop-up in searches for it though.

I'm not looking for a CodeSmith code generation technique or T4 template because I only want it to generate these specific lines inside my class, not generate and entire class or a separate file.

Does anyone know a way to press a few keystrokes, enter the "foo" and "moo" object names above and have the tool generate these copy from right to left lines of code?

Update:

I've found some documentation on building extensions to ReSharper, and this can probably be achieved by that path, but it looks really involved.

http://www.jetbrains.net/confluence/display/ReSharper/PowerToys+Pack+3.0+User+Guide

This is beginning to look like a weekend challenge unless someone else has already written it.

like image 274
a7drew Avatar asked Aug 04 '09 15:08

a7drew


4 Answers

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#.
like image 63
Jess Avatar answered Nov 14 '22 23:11

Jess


This is somewhat derivative from answer by @Jess (his regex didn't work for me on VS2013) but instead of using Visual Studio I am using regex101

Click link above and just paste your properties into Test string field and you will get them mapped.

Regex I used

public [A-Za-z\?]* ([A-Za-z0-9]*) .*

and replace

Dest.$1 = Source.$1

hope this saves you some time.

like image 20
Matas Vaitkevicius Avatar answered Nov 14 '22 22:11

Matas Vaitkevicius


I don't believe Resharper can do this, but Open Source AutoMapper can. New to AutoMapper? Check out the Getting Started page.

like image 9
Ben Griswold Avatar answered Nov 14 '22 23:11

Ben Griswold


I agree with @Ben Griswold.
In most situations, Automapper is the way to go.

But when you truly want to generate code that copies properties from one object to another, try this:

  1. Create a brand new class and derive from the class from which you want to copy properties.
  2. Right-click on this new derived class and click 'Refactor > Extract Interface'.
  3. Check all properties that you wish to copy.
  4. Choose 'Place beside' because this interface will be only temporary.
  5. Click 'Next'.
  6. Modify your derived class so that you are no longer inheriting from the base class and you are only implementing your new interface. Expect to see a red squiggle.
  7. Place your cursor over the red squiggle and hit 'ALT-ENTER' to 'Implement Members'.
  8. Click 'Finish'.
  9. Delete that temporary interface and modify your class so that you are no longer implementing it.
like image 4
Jim G. Avatar answered Nov 14 '22 23:11

Jim G.