5

I have got two user defined CLR objects which are identical and only differ in name, for example this code:

public class Person
{
  public string Name { get; set; }
  public string Address { get; set; }
  public string LastName { get; set; }
  public string ETC { get; set; }
}

public class PersonData
{
  public string Name { get; set; }
  public string Address { get; set; }
  public string LastName { get; set; }
  public string ETC { get; set; }
}

I know that this can be done by creating an object from either of these CLR's and than pass all properties one by one to it.

But is there another way? I got a few CLR's that are pretty big, 15+ properties.

[Edit] Some more context. The classes where already there. I generated a model from the database using EntityFramework. The database has almost the exact same structure as the classes.

Also, it's a lot of code that was already there. These classes also inherit from several interfaces etc. Refactoring now is not an option, so i'm looking for an easy fix for now.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Quoter
  • 4,236
  • 13
  • 47
  • 69
  • 2
    If you are sure their properties are exactly the same you can do that with a bit of reflection – BlackBear Sep 06 '13 at 21:45
  • Maybe by using `Reflection`, however why do you have such the same structure classes? – King King Sep 06 '13 at 21:45
  • Why doesn't your `Person` object hold an reference to `PersonData` instead? Aka Composition. (Assuming this is your use case). – Jeroen Vannevel Sep 06 '13 at 21:47
  • I could imagine the others being DTOs used to deliver the property values of the "original" objects via WCF, for example. – Jouni Aro Sep 06 '13 at 21:49
  • You do know you can map existing classes using EntityFramework, you don't need to use the generator over a database schema. A good chunk of EF functionality is there to enable this. To put it another way: EntityFramework is a "mapper-style" ORM, not a wrapper around database tables. It just happens to let you get by without any explicit mapping most of the time. – millimoose Sep 06 '13 at 21:56
  • @millimoose, no I did not? How? Tell me more please! – Quoter Sep 06 '13 at 21:58
  • 1
    @Quoter You need to create a model definition at runtime. I think the main approach is the "Fluent API". Tutorials are here: [properties and types](http://msdn.microsoft.com/en-us/data/jj591617), [relationships](http://msdn.microsoft.com/en-us/data/jj591620) You can also achieve some of this by using [annotations](http://msdn.microsoft.com/en-us/data/jj591583) on your existing classes. (That said, the fluent API should be able to do everything the annotations can and more. This also lets you map classes you can't modify.) – millimoose Sep 06 '13 at 22:01
  • @Quoter (This is a good idea why you should always say what you're ultimately trying to accomplish in your question. There might be a better way to do it than the approach you're pursuing.) – millimoose Sep 06 '13 at 22:05
  • The project as it is right now is too complex for the Code First approach. It kind of sounded lik that with FLuent API and data annotations. But thanks for your idea's and I will say what I'm ultimatly trying the next time I ask a question here :p – Quoter Sep 06 '13 at 22:08

3 Answers3

9

Assumming you don't want both classes to inherit from an interface you can try Automapper it's a library for automatically mapping similar classes.

Eli Algranti
  • 8,707
  • 2
  • 42
  • 50
5

If you are the author of those classes, you could have them implement the same interface, or even better, inherit from the same class. That way you don't have to copy values from one to another - just use a reference to their parent type. Remember, having couples of unrelated types which have exactly the same members is a sure sign that you need to rethink your design ASAP.

Geeky Guy
  • 9,229
  • 4
  • 42
  • 62
2

And, just for the sake of completeness, here's what it looks like with reflection:

var a = new Person( ) { LastName = "lastn", Name = "name", Address = "addr", ETC = "etc" };
var b = new PersonData( );

var infoPerson = typeof( PersonData ).GetProperties( );
foreach ( PropertyInfo pi in typeof( Person ).GetProperties( ) ) {
    object value = pi.GetValue( a, null );
    infoPerson.Single( p => p.Name == pi.Name )
        .SetValue( b, value, null );
}
BlackBear
  • 22,411
  • 10
  • 48
  • 86
  • @Quoter: How slow it is depends on your definition of slow. If you have to do thousands per second, you may consider it slow. If you need to do thousands per day, you would not. – Gabe Sep 06 '13 at 22:25
  • @Quoter I don't really know.. I'll do a benchmark soon and post the results here – BlackBear Sep 06 '13 at 23:30
  • The data that comes in is pretty hugh. But i'll give this approach a try too and see how this works, +1 thanks! – Quoter Sep 07 '13 at 08:05