T
Torben Laursen
I have a complicated class that I use to store a lot of values
Now I need a copy of that class and I have been trying to find a way to do
this automatic so I don't have to assign all properties and classes by hand
(about 200 properties).
I found this post on google:
http://www.thescripts.com/forum/thread225825.html
But how does this code work and what do I use instead of "oth" in the last
line
using System.Reflection;
class MyClass {
int a;
// lots of different fields goes here
float x;
MyClass() {
// default constructor
}
MyClass (MyClass my) {
// create an exact copy of mc here
FieldInfo[] fields = typeof(MyClass).GetFields(BindingFlags.Public
| BindingFlags.NonPublic | BindingFlags.Instance);
foreach(FieldInfo f in fields) {
f.SetValue(this,f.GetValue(oth));
}
}
}
Thansk Torben
Now I need a copy of that class and I have been trying to find a way to do
this automatic so I don't have to assign all properties and classes by hand
(about 200 properties).
I found this post on google:
http://www.thescripts.com/forum/thread225825.html
But how does this code work and what do I use instead of "oth" in the last
line
using System.Reflection;
class MyClass {
int a;
// lots of different fields goes here
float x;
MyClass() {
// default constructor
}
MyClass (MyClass my) {
// create an exact copy of mc here
FieldInfo[] fields = typeof(MyClass).GetFields(BindingFlags.Public
| BindingFlags.NonPublic | BindingFlags.Instance);
foreach(FieldInfo f in fields) {
f.SetValue(this,f.GetValue(oth));
}
}
}
Thansk Torben