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

Viewed 10778

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.

8 Answers

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

Here's a simple class to clone an object. It's not exactly what you asked for but perhaps this will be useful for you:

//.Net 2.0
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace YourNameSpace {
   public static class ObjectCloner {
      public static T Clone<T>(T obj) {
         using (MemoryStream buffer = new MemoryStream()) {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(buffer, obj);
            buffer.Position = 0;
            T temp = (T)formatter.Deserialize(buffer);
            return temp;
         }
      }
   }
}

Based on @Matas answer I created a more robust version using regex101 that ignores generics, attributes and comments and normalizes spaces.

Regex: *((\/+.*\n*.*)|(\[.*\]\n*.*))*public [A-Za-z\_\?\<\>]* ([A-Za-z0-9\_]*).*(\n| )*

Replace: $4 = person.$4,\n

This is the kind of thing for which Cog shines. Basically, Cog is code generation tool. Code is generated via Python.

Simply copying values from one side to the other is pretty ugly.

You might find it better to create a method to include in your classes that uses reflection to copy public properties. You could save this method in resharper to regenerate into other classes you need this functionality in.

Related