0

This may seem like a dumb question but I have reasons to want to copy a bunch of properties in one object A into another B, where A is a subset of B. This means when writing out the code in C#, it's difficult to be sure I have actually copied everything off A.

A. <=Intellisense gives me everything in A, which I don't care about.

B. <=great but I cannot finish typing in such a way that it will end up like this: A.prop = B.similarProp

So it's a picky thing, I just want to know how to write an extension method that will do this (I have literally no idea.)

B.something.GiveTo(A.somethingelse)

I DONT WANT TO USE AUTOMAPPER because it will error since B is a SUPERSET of A in this case.

I'm not trying to ask how to map properties of an object. I'm asking how to write something that will let me type A.propa.ExtensionMethodGiveTo(b.propa)

Worthy7
  • 1,455
  • 15
  • 28
  • 1
    Have you tried something, if so post your in-progress code – Sethu Bala Dec 16 '16 at 06:41
  • You want to copy values of similar attributes of A to B? – Prajwal Dec 16 '16 at 06:41
  • @Prajwal Yes the question says so. – Sethu Bala Dec 16 '16 at 06:42
  • I literally have no idea that's why I'm posting – Worthy7 Dec 16 '16 at 06:42
  • Not all attributes, that's for AutoMapper. I want to make typing these things easier. For example when updating an entity from an InputDto, that means I have to write out every map manually unless I want error from AutoMapper. I don't mind doing it, but it just takes ages – Worthy7 Dec 16 '16 at 06:43
  • 1
    Can be duplicate of http://stackoverflow.com/questions/16118085/best-practices-for-mapping-one-object-to-another (or any other "map properties of type A to B" question)... – Alexei Levenkov Dec 16 '16 at 06:43
  • I'm not trying to ask how to map properties of an object. I'm asking how to write something that will let me type `A.propa.giveto.(b.propa)` – Worthy7 Dec 16 '16 at 06:47

1 Answers1

0

You can get the list of properties in type A. (How to get the list of properties of a class?)

Create a generalised method to type A or B. (Generic Methods (C# Programming Guide))

Then compare the property type and name, if both are same, then you can assign that to passed instance.

Community
  • 1
  • 1
Prajwal
  • 3,930
  • 5
  • 24
  • 50
  • I did. So far, I'm sticking with the answer. If this is not what you expected, then please edit your question and explain a bit more. Please check http://stackoverflow.com/help/how-to-ask – Prajwal Dec 16 '16 at 07:59