Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reflection

Reflection

Scheduled Pinned Locked Moved C#
question
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Timothy_1982
    wrote on last edited by
    #1

    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 }

    G 1 Reply Last reply
    0
    • T Timothy_1982

      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 }

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Why not use the method MemberwiseClone? --- b { font-weight: normal; }

      T 1 Reply Last reply
      0
      • G Guffa

        Why not use the method MemberwiseClone? --- b { font-weight: normal; }

        T Offline
        T Offline
        Timothy_1982
        wrote on last edited by
        #3

        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

        G 1 Reply Last reply
        0
        • T Timothy_1982

          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

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          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; }

          T 1 Reply Last reply
          0
          • G Guffa

            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; }

            T Offline
            T Offline
            Timothy_1982
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups