How to get property values in this situation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kc0212
    New Member
    • Aug 2010
    • 6

    How to get property values in this situation

    Hi

    I got a question on how to get the property values using reflection in c#. (It's my first time to use, i think i lost somewhere...)

    I have compile an assembly and successfully deserialize a XML to it.

    By using 'InvokeMember' method, 'result' can hold the property of 'Deserializeobj ' (image enclosed : ref_Image1.jpg) . Then, i would like to ask how to further get all the values from the collection in 'result'?

    Thank you very much

    Code:
    Type[] ts = assembly.GetTypes();
    foreach (Type t in ts)
    {
                           
    //Create the object for deserialization
    object DeserializeObj = t.InvokeMember(t.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, new object[] { });
    
    //Read XML to stream reader
    StreamReader sr = new StreamReader(txtXMLPath.Text);
    
    //Deserialization
    DeserializeObj = new XmlSerializer(t).Deserialize(sr);
    
    //Get the properties of the object
    PropertyInfo[] pis = DeserializeObj.GetType().GetProperties();
    
    object result = new object();
    object result2 = new object();
    
    foreach (PropertyInfo pi in pis)
    {
    
    // Get property in the object
    result = DeserializeObj.GetType().InvokeMember(pi.Name, BindingFlags.GetProperty, null, DeserializeObj, new object[] { });
    
    result2 = result.GetType().InvokeMember(result.GetType().FullName, BindingFlags.ExactBinding, null, DeserializeObj, new object[] { 0 });
    
    }
    }
    Attached Files
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    I don't understand your question correctly. Did you mean, you don't get the right values out of your objects?
    If so, you could use pi.GetValue(Des erializeObj). This would return the property's value.

    Or did you mean, you get the right values, but they are collections (you wrote about something like that)?
    So you'd have to cast your result-variable to collection and iterate over it.
    E.g.
    Code:
      foreach(object o in System.Collections.IEnumerable)result) {
      //do sth...
    }
    But I don't know if this works if result's type is of any non-generic collection. Nevertheless I hope, this helps.

    Comment

    • kc0212
      New Member
      • Aug 2010
      • 6

      #3
      Sorry for the confusion on my question. Anyway, thanks for your reply
      I got the object and i can see the data in the collection. But, I don't know how to iterate to get the values. I tried your suggestion but it's not working as you said... it may due to the non-generic type... Actually, I'm trying to cast the collection to IEnumerable, ICollection, IList. However, still no luck on them. I would like to know any further suggestion? Thanks again on your kindly reply.

      Comment

      • Christian Binder
        Recognized Expert New Member
        • Jan 2008
        • 218

        #4
        Maybe you try also to cast it to Array
        What does actually happen?
        In your screenshot, I see result is of type bookstoreBook[], right?
        (maybe you could post a greater screenshot, it's very small for reading the text)

        Comment

        • kc0212
          New Member
          • Aug 2010
          • 6

          #5
          Yes, the type is bookstoreBook[]

          For IEnumerable, ICollection, IList, all of them giving similar behavior. I take the IList as an example. (please refer to Cast1.jpg).

          I'm not so sure how to cast to array, so I tried ArrayList instead. it can be used an index for getting the values. (Please refer to ArrayList1.jpg) . Is there any way to get the properties there?
          Attached Files

          Comment

          • kc0212
            New Member
            • Aug 2010
            • 6

            #6
            I'm afraid bytes.com decrease the image size.... sorry about that.
            please visit the links for the images. Thanks

            Cast1.jpg


            ArrayList1.jpg

            Comment

            • Christian Binder
              Recognized Expert New Member
              • Jan 2008
              • 218

              #7
              In your second image you have already your object like you're doing in immediate window.
              So what you want to do with this object? You can eighter convert it to bookstoreBook-object by casting or you print it's properties dynamically like you do it with the types of assembly.GetTyp es().
              I'm sorry, but I can't really figure out, what you're going to do with de deserialized data.

              Comment

              • kc0212
                New Member
                • Aug 2010
                • 6

                #8
                I'm confusing by this type and properties... sorry about that. After Casting to ArrayList, I can get the value using the getValue method.
                Actually, i would like put deserialized data into database. Are there any better way to do this?

                Comment

                • Christian Binder
                  Recognized Expert New Member
                  • Jan 2008
                  • 218

                  #9
                  After casting to ArrayList you can iterate over it, e.g. foreach(object bookstoreBook in alstObject).
                  After that you can work with your bookstoreBook-objects.
                  You'd have to read again all of its properties by using bookstoreBook.G etType().GetPro perties() as you did before.

                  Should your program be generic or do you use it only for you bookstoreBook-objects? If the last, it could be much easier if you cast your objects to bookstoreBook.

                  Comment

                  • kc0212
                    New Member
                    • Aug 2010
                    • 6

                    #10
                    Hi

                    It require dynamic deserialization . So, i can't cast it to bookstorebook.. .. I can use GetProperties() to retrieve the value now.

                    Anyway, Thank you for your kindly reply.

                    Comment

                    Working...