473,386 Members | 1,969 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,386 software developers and data experts.

using reflection to discover a nested structure

All,

Can anyone supply an example or reference to an example of using
reflection to determine the data types contained in a nested stucture
in C#? Once I get the list of MemberInfo[] and determine that
MemberInfo[i].MemberType.ToString().Equals("NestedType"), I cannot
figure out how to drill down from that point.
TIA,
Bill

Oct 25 '06 #1
2 4869
bill wrote:
All,

Can anyone supply an example or reference to an example of using
reflection to determine the data types contained in a nested stucture
in C#? Once I get the list of MemberInfo[] and determine that
MemberInfo[i].MemberType.ToString().Equals("NestedType"), I cannot
figure out how to drill down from that point.
You would have to use Activator.CreateInstance to get a reference to
the NestedType and then use the same method to drill down.

I think it would be something like this:

ClassA aObj = new ClassA();
Type t = aObj.GetType();

ClassB b = t.InvokeMember("membername", BindingFlags.GetProperty, null,
aObj, null) as ClassB;

So if "membername" is a property of ClassA that is an instance of
ClassB, the InvokeMember line will get a reference to that member.

I'm now sure how you would change this for your NestedType, but it
should be similar.

Chris

Oct 25 '06 #2
Hi Bill,

Think of it like this: If you knew that a MemberInfo instance was a property,
you would cast it to PropertyInfo. And if a MemberInfo was a field, then you
could cast it to FieldInfo. So what do you cast a MemberInfo to that is a
nested Type? Type. (TypeInfo would be semantically equivalent to Type, so
there is no need for a TypeInfo class :)

class Outer
{
public class Inner
{
public int AField;
}
}

class Program
{
static void Main()
{
foreach (MemberInfo member in typeof(Outer).GetMembers())
{
if (member.MemberType == MemberTypes.NestedType)
{
// MemberInfo that is a NestedType is just a Type itself
Type nestedType = (Type) member;

Console.WriteLine("Members of {0}: ", nestedType);
Console.WriteLine();

foreach (MemberInfo nestedMember in nestedType.GetMembers())
{
Console.WriteLine("Nested Member: " + nestedMember.Name);
}
}
}
}

--
Dave Sexton

"bill" <wg****@draper.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
All,

Can anyone supply an example or reference to an example of using
reflection to determine the data types contained in a nested stucture
in C#? Once I get the list of MemberInfo[] and determine that
MemberInfo[i].MemberType.ToString().Equals("NestedType"), I cannot
figure out how to drill down from that point.
TIA,
Bill

Oct 25 '06 #3

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

Similar topics

5
by: Andre | last post by:
I have two questions; 1) When executing the sub routine "TestStructure", I'm trying to update the member "intTyres" in the structure "structureCar" dynamically using System.Reflection. The code...
8
by: Robert W. | last post by:
I've almost completed building a Model-View-Controller but have run into a snag. When an event is fired on a form control I want to automatically updated the "connnected" property in the Model. ...
2
by: Robert W. | last post by:
I'm trying to write a utility that will use Reflection to examine any data model I pass it and correctly map out this model into a tree structure. When I say "any" , in fact there will only be 3...
2
by: Mark | last post by:
Am I out of my mind if I use Reflection everytime someone logs into our site to get and track the current Major/Minor/Build/Revision version that the person is viewing our site through? This...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
0
by: StuartJSmith | last post by:
Hi, I am writing a control whereby I wish to be able to run csharp commands dynamically at runtime - this is easy when it is in the context of say, one form. I can have a reference to that form...
5
by: heddy | last post by:
I understand that reflection allows me to discover the metadata of a class at runtime (properties, methods etc). What I don't understand is where this is useful. For example: If I am the sole...
3
by: Steve Amey | last post by:
Hi all I am using reflection to read the values of properties from a class. The class is returned from a Web Service so I have to access the class using FieldInfo (Using VS 2003 which converts...
9
by: Bill Grigg | last post by:
All, Can anyone supply an example or reference to an example of using reflection to determine the data types and array lengths contained in a nested stucture in C#? Actually, it is a structure...
9
by: =?Utf-8?B?VmljdG9y?= | last post by:
Is it a way to discover, at the run time, the name of a property of an object? In other words is it possible to create a method GetPropertyName, that takes a property of an object and returns the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.