473,770 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ToS tring().Equals( "NestedType "), I cannot
figure out how to drill down from that point.
TIA,
Bill

Oct 25 '06 #1
2 4889
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.ToS tring().Equals( "NestedType "), I cannot
figure out how to drill down from that point.
You would have to use Activator.Creat eInstance 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.Ge tProperty, 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).G etMembers())
{
if (member.MemberT ype == MemberTypes.Nes tedType)
{
// MemberInfo that is a NestedType is just a Type itself
Type nestedType = (Type) member;

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

foreach (MemberInfo nestedMember in nestedType.GetM embers())
{
Console.WriteLi ne("Nested Member: " + nestedMember.Na me);
}
}
}
}

--
Dave Sexton

"bill" <wg****@draper. comwrote in message
news:11******** **************@ h48g2000cwc.goo glegroups.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.ToS tring().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
451
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 executes, but the value does not change, nor is there an exception thrown. 2) Read comments in sub routine "TestIntegerProperty"
8
16920
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. This works fine if all of the properties are at the top (root level) of the model but I'd like to keep them in nested classes to organize them better. So, for example, part of my data model looks like this (simplified) : public class MainClass
2
2987
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 types of items in the very hierarchical data model: - Classes (and nested classes) - Collections - Properties I've successfully written the Reflection code to handle any combination of classes and properties but I'm confused about what to do...
2
2014
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 information would then be logged to a database along with some other information about the user. Thanks in advance. Mark
13
12392
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
288
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 in my control and then use reflection to get all the methods on it to display some sort of intellisense thing etc... What I want to be able to do however is discover loaded public classes at runtime - which may or may not be forms...
5
1863
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 author of an application, I already know the metadata about my classes - I wrote them. And even if I did not - Let's say I buy a 3rd party class lib, it will be documented for me with that lib. If I am part of a dev team, the same is true - If...
3
4101
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 the Properties into Fields when it comes out of the Web Service). I have this at the moment: Private _aDataSource As Object 'Person class
9
2397
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 that I use to communicate to some unmanaged code in a DLL written in C. It is not complicated, but will change and I would like to be able to sequentially access it without explicitly referring to each and every element. Here is the structure:
9
1943
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 name of that property? So: string name = GetPropertyName(int.MaxValue); // returns “MaxValue” string name = GetPropertyName(DateTime.Now); // returns “Now” (Something similar exists to...
0
9617
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9453
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9904
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7451
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4007
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
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.