473,503 Members | 2,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I get Array elements from Reflection C#

How do I get Array elements value from Reflection..

Here is the C# code..

I have 2 struc

Public struct Address {
public string city;
}

public struct Person {
public Address[] Experience;
}
Person p = new Person();
p.Experience = new Address[2];
p.Experience[0] = "Los Angeles";
p.Experience[1] = "NewYork";
Type personInfo = p.GetType();
FieldInfo[] fieldInfo = personInfo.GetFields();

foreach(FieldInfo info in fieldInfo) {
if (info.FieldType.IsArray) {
Object Temp = (Object)info.GetValue(sender);
int len = ((Array)Temp).Length;

// Error here. Error is cannot convert Address type to Object type
Object[] a = (Object[])Temp;
// If I do like "Address[] a = (Address[])Temp;" then
it works...But this class is a
helper class for serialization like JSON. So Object can be any
type.

for (int i = 0; i < len; i++)
result = a[i].City;
}
}

Anyone have any better idea.
All I need is to get object field name and its value.

thanks in advance.

achu.

Apr 17 '06 #1
5 5292
How do I get Array elements value from Reflection..


In this case I think you can use System.Array.GetValue().
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Apr 18 '06 #2
<as******@gmail.com> wrote:
How do I get Array elements value from Reflection..


You can cast to Array and then use Array.GetValue to get a value.

The reason your cast to object[] didn't work is that Person is a value
type, and array covariance is only in terms of reference types (so a
string[] can be cast to an object[], but an int[] can't).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 18 '06 #3
Hi,

Thats what I was looking for. Thank you very much for the input both
Jon and Mattias.
Quick question.
How do I find FieldType is a custom class or struct other than system
generic types. I could use "System.String", "System.Int32" etc.. Any
shot cuts ? Temp I use dirty way. Type.namespace != "System"

thanks
regards,
achu.

Apr 18 '06 #4
<as******@gmail.com> wrote:
Thats what I was looking for. Thank you very much for the input both
Jon and Mattias.
Quick question.
How do I find FieldType is a custom class or struct other than system
generic types. I could use "System.String", "System.Int32" etc.. Any
shot cuts ? Temp I use dirty way. Type.namespace != "System"


I'm not entirely sure what you mean. If you give a very precise
definition of exactly what you want the criteria to be, I suspect
you'll find that translates into code fairly naturally.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 18 '06 #5
hi Jon,

How do I differentiate built in C# data types (Int32, String, DateTime
etc..) and user defind data types (Eg. "Address Struct") using
reflection? My Idea is to get all custom defind struct and class.

thanks,
Achu.

Apr 18 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
3182
by: BCC | last post by:
Ive been googling and reading through my books but I haven't figured out a solution (much less an elegant one) to create a multidimensional array with a runtime determined number of dimensions. I...
4
1797
by: Laszlo Szijarto | last post by:
I'm iterating through various members of a struct using Reflection. While I can use fieldInfo.FieldType.BaseType to determine that a member is an array, how do I determine what that array is an...
4
1995
by: Wernfried Schwenkner | last post by:
I want to deserialize a file. In one ofe my classes I have an array like: public class MyClass { public MyArray theArray; } i.e. an arry of an own defined class. While reading the file I...
3
4190
by: trevorelbourne | last post by:
Hi, I am having trouble accessing the elements of an array using reflection. This is the code I am having trouble with: FieldInfo Fields = Obj.GetType().GetFields(); foreach (FieldInfo fi in...
1
6587
by: Taiwo | last post by:
I generated a Typed Dataset class including a base64Binary column. This column was specified as a .NET type of Byte() in the class that was auto-generated. I set the value of this property to New...
45
4761
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
3
4279
by: bill | last post by:
All, I have the following: ...... unsafe public struct Example { public double we;
14
12392
by: dan | last post by:
I would like to have the preprocessor automatically generate the number of array elements requested. Each element is zero. The elements get pasted into a larger array. The other elements may be...
2
6102
by: Fred Mellender | last post by:
I am trying to use reflection to output the fields (names and values) of an arbitrary object -- an object dump to a TreeView. It works pretty well, but I am having trouble with generic lists,...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7348
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4685
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
397
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.