Cast one object to another

Quoter

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.

Eli Algranti

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cast one object to another

From Dev

How to cast one object into another

From Dev

Trying to cast one object type into another in Python

From Dev

How to cast a object into another?

From Dev

Scala Cast object to another type

From Dev

Three.js | Is it possible to cast a ray from one object to another using raycaster?

From Dev

Pull an object from a hashtable and cast it to another object

From Dev

Cast one class object to other class object

From Dev

Instantiating an object in another one

From Java

Typescript - clone an object and cast into another type

From Dev

Cast object to another type at run time

From Dev

Converting object of a class to of another one

From Dev

JS copy one object to another

From Dev

Complex object inside another one

From Dev

Create one object and update another in one request

From Dev

Same object in one case, different object in another?

From Dev

add one object to another object in javascript

From Dev

Cast one pointer-to-member-function to another of same class

From Dev

Why can't I cast one instantiation of a generic type to another?

From Dev

How to cast / assign one enum value to another enum

From Dev

Why is the compiler allowing me to cast one implementing class to another?

From Dev

Why does one interface implementation require a cast while another does not?

From Dev

Cast using one variable as column name and another as a value source in R

From Dev

How can I cast an object with fields missing to another in Typescript?

From Dev

Class cast Exception while sending object to another activity

From Dev

C# cast object to another type stored in string variable

From Dev

C# cast object to another type stored in string variable

From Dev

How can I cast an object with fields missing to another in Typescript?

From Dev

How can I cast an object in one line of code and call a function?

Related Related

  1. 1

    Cast one object to another

  2. 2

    How to cast one object into another

  3. 3

    Trying to cast one object type into another in Python

  4. 4

    How to cast a object into another?

  5. 5

    Scala Cast object to another type

  6. 6

    Three.js | Is it possible to cast a ray from one object to another using raycaster?

  7. 7

    Pull an object from a hashtable and cast it to another object

  8. 8

    Cast one class object to other class object

  9. 9

    Instantiating an object in another one

  10. 10

    Typescript - clone an object and cast into another type

  11. 11

    Cast object to another type at run time

  12. 12

    Converting object of a class to of another one

  13. 13

    JS copy one object to another

  14. 14

    Complex object inside another one

  15. 15

    Create one object and update another in one request

  16. 16

    Same object in one case, different object in another?

  17. 17

    add one object to another object in javascript

  18. 18

    Cast one pointer-to-member-function to another of same class

  19. 19

    Why can't I cast one instantiation of a generic type to another?

  20. 20

    How to cast / assign one enum value to another enum

  21. 21

    Why is the compiler allowing me to cast one implementing class to another?

  22. 22

    Why does one interface implementation require a cast while another does not?

  23. 23

    Cast using one variable as column name and another as a value source in R

  24. 24

    How can I cast an object with fields missing to another in Typescript?

  25. 25

    Class cast Exception while sending object to another activity

  26. 26

    C# cast object to another type stored in string variable

  27. 27

    C# cast object to another type stored in string variable

  28. 28

    How can I cast an object with fields missing to another in Typescript?

  29. 29

    How can I cast an object in one line of code and call a function?

HotTag

Archive