1

Consider below two class:

public class AAA{
   string _Test1;
   public string Test1{
   get=>_Test1;
   set{_Test1=value;}
   }
}

public class BBB:AAA{
  string _Test2;
   public string Test2{
   get=>_Test2;
   set{_Test2=value;}
   }
}

And here are the Objects:

AAA aaa=new AAA(){Test1="123"};
BBB bbb=new BBB(){Test2="456"};

I want to copy all the value of aaa to the child class bbb.

In fact, there are many properties in the base class object and I don't want to copy the values manually, one by one :

bbb.Test1=aaa.Test1;


How can I do it? Would you please help me? Thank you.

102425074
  • 781
  • 1
  • 7
  • 23
  • 2
    Seems like a classic use-case for [AutoMapper.](https://automapper.org/) – Zohar Peled May 09 '19 at 08:12
  • 1
    what you actually want to do is to copy all values from an object of type base class to **a different** object from type child class. Did I understand you correctly? up to now your title is missleading, because if I take it litteraly then the answer would be you don't need to, they are all inherited! – Mong Zhu May 09 '19 at 08:12
  • 4
    If I were you, I'd take a step back and ask why you're trying to do this in the first place. to me this looks like a bit of a code smell. – DavidG May 09 '19 at 08:13
  • @ZoharPeled Maybe it can, but I wanna find something original C# code to do it. – 102425074 May 09 '19 at 08:14
  • I don't understand what you mean by "something original c# code". There's nothing built in the framework that does it, that's why we have 3rd party mapping solutions such as automapper in the first place. – Zohar Peled May 09 '19 at 08:17
  • @MongZhu I am afraid did not explain clearly and it seems someone edit the question for me. Something that I want to do is just what you said. – 102425074 May 09 '19 at 08:19
  • It seems like you confuse classes with *instances* of classes. `bbb` is **not** a "child class". It's an *instance* of `BBB` and `BBB` is a child class of `AAA`. The two *instances* `aaa` and `bbb` are completely separate things. – Corak May 09 '19 at 08:20
  • @Corak Yes, just what you said. And now someone helped me to edit the question yet. – 102425074 May 09 '19 at 08:42
  • please be precise when describing your problem! In your title you write: "How to copy **properties**..." But in your code you post **fields** ! What is it then? – Mong Zhu May 09 '19 at 08:49

1 Answers1

1

You can use reflection to do this:

foreach (var field in typeof(AAA).GetFields())
{
    field.SetValue(bbb, field.GetValue(aaa));
}

The idea is to loop through all the fields of type AAA in bbb and assign to them the values that they had in aaa. Note that this will only work for fields; if you want to also copy the values of properties, you can extend this to also use the .GetProperties() method.

Philip Atz
  • 886
  • 1
  • 10
  • 26
  • I tried your ways. However, it is so strange that it reports no error but does not copy correctly. All the values in bbb are the default value but not the value in aaa. – 102425074 May 09 '19 at 08:36
  • @102425074 you need to be precise when describing your situation. Do you really have **fields** in your class? or do you have **properties**? This make a huge difference for reflection – Mong Zhu May 09 '19 at 08:46
  • string _Source; public string Source { get => _Source; set { _Source = value; OnPropertyRaised("Source"); } }. Here is an example of my fileds.@MongZhu – 102425074 May 09 '19 at 08:48
  • 1
    @102425074 these are properties, the difference can be [read here](https://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property). You would need to use the method: `GetProperties()` – Mong Zhu May 09 '19 at 08:51
  • @MongZhu After I change the code as what you said, it works. Thank you so much and I think I should read more about the difference between Property and Field. – 102425074 May 09 '19 at 08:52
  • 1
    @102425074 Note that reflection like this can be very slow which is why libraries like AutoMapper exist. Also you will be calling all of the `OnPropertyRaised` events for *every property you set* - that is often a huge performance issue. It might be annoying, but doing it manually might be the safest and most performant option. – DavidG May 09 '19 at 08:54
  • @102425074 you should addapt your posted code that it reflects your situation at hand / your real code. Make them to properties! – Mong Zhu May 09 '19 at 08:54
  • 1
    @MongZhu I am afraid about making so more trouble for mistaking them as the same thing. Now I edited it and about to read more about the difference between field and property. – 102425074 May 09 '19 at 09:04