views:

183

answers:

5

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.

+4  A: 

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

Ben Griswold
I had never heard of this tool. With data access layers and Data Contracts making this kind of code a necessity, this is a great tool!
Vaccano
A: 

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.

Bryan Rowe
A: 

How many properties are you trying to copy? How often do you have to write this copy code? Would it be easier to write a clone method inside the class (or extension method if the class is sealed) that would create a new object of the same type and copy the properties over, so that you only have the copy code in one place and can generate as many duplicate objects as necessary?

Jason Miesionczek
Ok, so you're suggesting a clone method. That's cool, but I still need to write that clone method, right? I would use this code-snippet-like extension (of ReSharper?) to generate most, but not all of the lines of code for the clone method. That's the challenge i'm trying to solve.
AndrewDotHay
seems to me that the amount of time your spending trying to solve this problem by being lazy could have been better spent actually writing the code and getting on with your life.
Jason Miesionczek
Hard to say. Most of what I've learned about software development that did me any good came from being lazy.
Robert Rossney
A: 

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;
         }
      }
   }
}
Chris Dunaway
A: 

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

Brian
So, your suggesting the answer to my question is to use a Python program to help me write my C# code? Do you have an example of this?
AndrewDotHay