473,320 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to get property values in this situation

6
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. Type[] ts = assembly.GetTypes();
  3. foreach (Type t in ts)
  4. {
  5.  
  6. //Create the object for deserialization
  7. object DeserializeObj = t.InvokeMember(t.Name, BindingFlags.Public | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, new object[] { });
  8.  
  9. //Read XML to stream reader
  10. StreamReader sr = new StreamReader(txtXMLPath.Text);
  11.  
  12. //Deserialization
  13. DeserializeObj = new XmlSerializer(t).Deserialize(sr);
  14.  
  15. //Get the properties of the object
  16. PropertyInfo[] pis = DeserializeObj.GetType().GetProperties();
  17.  
  18. object result = new object();
  19. object result2 = new object();
  20.  
  21. foreach (PropertyInfo pi in pis)
  22. {
  23.  
  24. // Get property in the object
  25. result = DeserializeObj.GetType().InvokeMember(pi.Name, BindingFlags.GetProperty, null, DeserializeObj, new object[] { });
  26.  
  27. result2 = result.GetType().InvokeMember(result.GetType().FullName, BindingFlags.ExactBinding, null, DeserializeObj, new object[] { 0 });
  28.  
  29. }
  30. }
  31.  
Attached Images
File Type: jpg ref_Image1.jpg (61.8 KB, 279 views)
Aug 18 '10 #1

✓ answered by Christian Binder

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.GetType().GetProperties() 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.

9 2423
Christian Binder
218 Expert 100+
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(DeserializeObj). 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.
Expand|Select|Wrap|Line Numbers
  1.   foreach(object o in System.Collections.IEnumerable)result) {
  2.   //do sth...
  3. }
  4.  
But I don't know if this works if result's type is of any non-generic collection. Nevertheless I hope, this helps.
Aug 18 '10 #2
kc0212
6
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.
Aug 18 '10 #3
Christian Binder
218 Expert 100+
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)
Aug 18 '10 #4
kc0212
6
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 Images
File Type: jpg ArrayList1.jpg (43.8 KB, 376 views)
File Type: jpg Cast1.jpg (45.3 KB, 238 views)
Aug 18 '10 #5
kc0212
6
I'm afraid bytes.com decrease the image size.... sorry about that.
please visit the links for the images. Thanks

Cast1.jpg
http://picasaweb.google.com.hk/10972...82077224095186

ArrayList1.jpg
http://picasaweb.google.com.hk/10972...82075449908994
Aug 18 '10 #6
Christian Binder
218 Expert 100+
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.GetTypes().
I'm sorry, but I can't really figure out, what you're going to do with de deserialized data.
Aug 19 '10 #7
kc0212
6
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?
Aug 19 '10 #8
Christian Binder
218 Expert 100+
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.GetType().GetProperties() 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.
Aug 19 '10 #9
kc0212
6
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.
Aug 21 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: windandwaves | last post by:
Hi Folk I am working with the TYPE property. I want to change that from 2 (byte) to 4 (long integer) for a field that already contains tons of data. How can I do that? The help reads: ...
2
by: Balamurukan | last post by:
How to retrive property values from our own property window
0
by: Guy Bradley | last post by:
Hi, I'm using a couple of classes to store property values (I'm using the property grid control to allow editing of each classes values) - some of my classes inherit properties from properties...
7
by: David Lozzi | last post by:
Howdy, I have a usercontrol in my aspx page and when the page loads, I send it some property values based on the data displayed. I am sending it to a public property like below. However the...
3
by: Gugale at Lincoln | last post by:
I am using a function to get properies and property values of an object private string ObjPropVals(Object o) { Type t = o.GetType(); PropertyInfo pia = t.GetProperties(); string stra = new...
8
by: stephen | last post by:
Hi, I make use of properties to store values that are relevant to the page but this time I am posting the page to itself and the values are Zero or null (they disappear). is there any other way...
1
by: David Veeneman | last post by:
I'm backporting a component to .NET 1.x, and it required me to use a custom collection, StringList, instead of List<string>. StringList is derived from CollectionBase and is marked serializable. ...
2
by: | last post by:
I want to use codebehind to pass property values to a control that I've embedded INSIDE of a user control. In other words, let's say I have the following: MyPage.aspx ....with the following...
5
by: VMI | last post by:
How can I pass variable or Property values from my popup webForm to the webform that opened this popup? For information that's in a control (i.e. Textbox), I'm using javascript and...
14
by: Mohamed Mansour | last post by:
Hey there, this will be somewhat a long post, but any response is appreciated! I have done many PInvoke in the past from C++ to C#, but I did PInvoke within C# not C++/CLI. Can someone explain...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.