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. How to set an item in a object's property(of array type) using Reflection

How to set an item in a object's property(of array type) using Reflection

Scheduled Pinned Locked Moved C#
csharpdata-structurestutorial
6 Posts 3 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.
  • H Offline
    H Offline
    here2learn
    wrote on last edited by
    #1

    Hi All, I have a class named "C1" which has a property name "P1", which is declared as below in class. Class C1 { ... Account1[] P1;// Here Account1 is another class/type ... } Now i have created an object of "C1" using reflection as below. // object Type objTempType = Type.GetType("C1"); object objTempObject = Activator.CreateInstance(objTempType); Now i want to set value of first item in "P1" property(An array property) of objTempObject using reflection, which i can do without reflection as per below. objTempType.P1[0].SomeFieldinAccount1Class = SomeObjectOrValue; Could any one of you please give me some direction on how is that possible. I checked all the possible ways do it using property/reflection related classes in C#, also searched many sites and forums to get answer but without any success...

    "You just become, like a flower becomes the fruit. It's all built in within you. Allow it to work out." for more details visit : http://www.freemeditation.ca/ http://www.sahajayoga.org Akhilesh Singh

    modified on Wednesday, August 26, 2009 5:41 AM

    N M 2 Replies Last reply
    0
    • H here2learn

      Hi All, I have a class named "C1" which has a property name "P1", which is declared as below in class. Class C1 { ... Account1[] P1;// Here Account1 is another class/type ... } Now i have created an object of "C1" using reflection as below. // object Type objTempType = Type.GetType("C1"); object objTempObject = Activator.CreateInstance(objTempType); Now i want to set value of first item in "P1" property(An array property) of objTempObject using reflection, which i can do without reflection as per below. objTempType.P1[0].SomeFieldinAccount1Class = SomeObjectOrValue; Could any one of you please give me some direction on how is that possible. I checked all the possible ways do it using property/reflection related classes in C#, also searched many sites and forums to get answer but without any success...

      "You just become, like a flower becomes the fruit. It's all built in within you. Allow it to work out." for more details visit : http://www.freemeditation.ca/ http://www.sahajayoga.org Akhilesh Singh

      modified on Wednesday, August 26, 2009 5:41 AM

      N Offline
      N Offline
      Nissim Salomon
      wrote on last edited by
      #2

      Hi Before you can set the SomeFieldinAccount1Class property/field value you need to intialize / create the element at position 0

      objTempType.P1[0] = new Account1();

      H 1 Reply Last reply
      0
      • N Nissim Salomon

        Hi Before you can set the SomeFieldinAccount1Class property/field value you need to intialize / create the element at position 0

        objTempType.P1[0] = new Account1();

        H Offline
        H Offline
        here2learn
        wrote on last edited by
        #3

        Hello nissims, Thanks for your reply. i have already created the first item in P1[0](before i try setvalue thing), I just didn't want to make my post a more lengthier one, so made the code hide.

        "You just become, like a flower becomes the fruit. It's all built in within you. Allow it to work out." for more details visit : http://www.freemeditation.ca/ http://www.sahajayoga.org Akhilesh Singh

        modified on Wednesday, October 7, 2009 4:23 AM

        1 Reply Last reply
        0
        • H here2learn

          Hi All, I have a class named "C1" which has a property name "P1", which is declared as below in class. Class C1 { ... Account1[] P1;// Here Account1 is another class/type ... } Now i have created an object of "C1" using reflection as below. // object Type objTempType = Type.GetType("C1"); object objTempObject = Activator.CreateInstance(objTempType); Now i want to set value of first item in "P1" property(An array property) of objTempObject using reflection, which i can do without reflection as per below. objTempType.P1[0].SomeFieldinAccount1Class = SomeObjectOrValue; Could any one of you please give me some direction on how is that possible. I checked all the possible ways do it using property/reflection related classes in C#, also searched many sites and forums to get answer but without any success...

          "You just become, like a flower becomes the fruit. It's all built in within you. Allow it to work out." for more details visit : http://www.freemeditation.ca/ http://www.sahajayoga.org Akhilesh Singh

          modified on Wednesday, August 26, 2009 5:41 AM

          M Offline
          M Offline
          Mirko1980
          wrote on last edited by
          #4

          You can use the Array class when operating on array properties with reflection. Using only refection, what you want to achieve is done like this:

          Type objTempType = Type.GetType("C1");
          object objTempObject = Activator.CreateInstance(objTempType);

          PropertyInfo p1Property = objTempType.GetProperty("P1");
          Array p1ArrayValue = p1Property.GetValue(objTempObject, null) as Array;

          Type itemType = p1ArrayValue.GetType().GetElementType();
          object itemValue = p1ArrayValue.GetValue(0);

          PropertyInfo someFieldProperty = itemType.GetProperty("SomeFieldinAccount1Class");
          someFieldProperty.SetValue(itemValue, SomeObjectOrValue, null);

          H 1 Reply Last reply
          0
          • M Mirko1980

            You can use the Array class when operating on array properties with reflection. Using only refection, what you want to achieve is done like this:

            Type objTempType = Type.GetType("C1");
            object objTempObject = Activator.CreateInstance(objTempType);

            PropertyInfo p1Property = objTempType.GetProperty("P1");
            Array p1ArrayValue = p1Property.GetValue(objTempObject, null) as Array;

            Type itemType = p1ArrayValue.GetType().GetElementType();
            object itemValue = p1ArrayValue.GetValue(0);

            PropertyInfo someFieldProperty = itemType.GetProperty("SomeFieldinAccount1Class");
            someFieldProperty.SetValue(itemValue, SomeObjectOrValue, null);

            H Offline
            H Offline
            here2learn
            wrote on last edited by
            #5

            Thanks Bro... your suggestion exactly matched what i needed and was more than i expected... thanks again... :)

            "You just become, like a flower becomes the fruit. It's all built in within you. Allow it to work out." for more details visit : http://www.freemeditation.ca/ http://www.sahajayoga.org Akhilesh Singh

            modified on Wednesday, October 7, 2009 4:23 AM

            M 1 Reply Last reply
            0
            • H here2learn

              Thanks Bro... your suggestion exactly matched what i needed and was more than i expected... thanks again... :)

              "You just become, like a flower becomes the fruit. It's all built in within you. Allow it to work out." for more details visit : http://www.freemeditation.ca/ http://www.sahajayoga.org Akhilesh Singh

              modified on Wednesday, October 7, 2009 4:23 AM

              M Offline
              M Offline
              Mirko1980
              wrote on last edited by
              #6

              You welcome :)

              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