Reflection
-
Reflection i have this class class A { public int a } now this is what i want to do in my method private A copy(A first) { A second as new A() now i want to copy all values from "first" tot "second" , like this i mean: second.a = first.a but on this way i don't want to work, when a new member is added, i always have to adjust this function so i want to loop all members with reflection and then copy the value like this: Type typea = Type.GetType("A") For Each member As System.Reflection.MemberInfo In typeA.GetMembers(Reflection.BindingFlags.DeclaredOnly) 'and here i want something like this: (offcourse this doesn't work) second.member.name.value = first.member.name .value --> how can i get something that does this with reflection Next }
-
Reflection i have this class class A { public int a } now this is what i want to do in my method private A copy(A first) { A second as new A() now i want to copy all values from "first" tot "second" , like this i mean: second.a = first.a but on this way i don't want to work, when a new member is added, i always have to adjust this function so i want to loop all members with reflection and then copy the value like this: Type typea = Type.GetType("A") For Each member As System.Reflection.MemberInfo In typeA.GetMembers(Reflection.BindingFlags.DeclaredOnly) 'and here i want something like this: (offcourse this doesn't work) second.member.name.value = first.member.name .value --> how can i get something that does this with reflection Next }
-
yezz i also tought of that but what will happen with arrays of reference types? class A { public z = new Z[4] } class Z { public h = new H[3] } class H { public int i } how does memberwizeclone clone this? my new object may not referece to the same objects, never!! because i will change some values, and my source object may not change along with it
-
yezz i also tought of that but what will happen with arrays of reference types? class A { public z = new Z[4] } class Z { public h = new H[3] } class H { public int i } how does memberwizeclone clone this? my new object may not referece to the same objects, never!! because i will change some values, and my source object may not change along with it
Then you have to loop through the members of the class and create a copy of each member that is a reference type. The code has to handle all data types that you will be using in the class. I would have let the class implement IClonable instead. --- b { font-weight: normal; }
-
Then you have to loop through the members of the class and create a copy of each member that is a reference type. The code has to handle all data types that you will be using in the class. I would have let the class implement IClonable instead. --- b { font-weight: normal; }
can't do, i don't have control over the class, i'm only using it so i think the only way doing this is with reflection, so if someone knows how i have to code this, please