473,569 Members | 2,700 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.GetF ields();

foreach(FieldIn fo info in fieldInfo) {
if (info.FieldType .IsArray) {
Object Temp = (Object)info.Ge tValue(sender);
int len = ((Array)Temp).L ength;

// 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 5323
How do I get Array elements value from Reflection..


In this case I think you can use System.Array.Ge tValue().
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.co m>
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.Int 32" 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.Int 32" 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.co m>
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
3190
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 also checked out the boost::multi_array.hpp, and Giovanni Bavistrelli's Array code. Neither of these seem to allow dynamic array dimensions. For...
4
1799
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 array of. e,g., I want it to tell me that a UInt32 member is an Array of UInt32 (via code). Thank you, Laszlo
4
1997
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 only know the dimension on the end of reading, so I keep the array elements in an
3
4196
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 Fields) { Object Temp = fi.GetValue(Obj); if (Temp.GetType().IsArray)
1
6592
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 Byte() {} (i.e. Byte array with zero elements) in a row of an instance of the class prior to serialization using the binary formatter. When I attempt...
45
4799
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 "new Array() vs " question or to the array performance, I dared to move it to a new thread. Gecko takes undefined value strictly as per Book 4,...
3
4293
by: bill | last post by:
All, I have the following: ...... unsafe public struct Example { public double we;
14
12408
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 non-zero. ***** Here is an example of what I need to do: #define YEAR_1 2005 #define YEAR_2 2007 #define YEARS (YEAR_2 - YEAR_1 + 1)
2
6119
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, like List<char>. What I have working is : Type type = newObj.GetType(); //newObj is what I'm trying to dump
0
7698
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8122
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7673
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6284
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.