473,386 Members | 1,803 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.

reflection problem

I need to print Public Properties of a class.
I must access to Properties with GetSubObject function.
Please help me.
FieldInfo[] fields=typeof(Boolean).GetFields();

ArrayList param=new ArrayList();

param.Add("Name");

This don't work!

foreach (Object iter in fields)
{
Object obj=Util.GetSubObject(param,iter);
if (obj!=null) Console.WriteLine((String) obj);
}
Console.ReadLine();

public static Object GetSubObject(ArrayList paramArray,Object
currentObj)
{
Object obj=null;
foreach (Object param in paramArray)
{
// scorro i parametri dell'oggetto
foreach( FieldInfo field in currentObj.GetType().GetFields())
{
if (field.Name.Equals(param))
{
obj=field.GetValue(currentObj);
if (obj!=null) currentObj=obj;
else return null;
}
//else return false;
}
}
return obj;
}

Nov 17 '05 #1
10 1561
Locia <ro********@yahoo.it> wrote:
I need to print Public Properties of a class.
I must access to Properties with GetSubObject function.
Please help me.

FieldInfo[] fields=typeof(Boolean).GetFields();


Are you actualy trying to access properties or fields? Your question
asks about properties, but your code only uses fields.

--
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
Nov 17 '05 #2
I would like do it
for (int i=0;i<fields.Length;i++)
Console.WriteLine(fields[i].Name);

but rather use fields[i].Name I must access to fields[i].Name with
GetSubObject("Name",fields[i]) function and using reflection?

Nov 17 '05 #3
Sorry It's a assertion not a question.I put a question mark but it's a
error.

Nov 17 '05 #4
Locia <ro********@yahoo.it> wrote:
I would like do it
for (int i=0;i<fields.Length;i++)
Console.WriteLine(fields[i].Name);

but rather use fields[i].Name I must access to fields[i].Name with
GetSubObject("Name",fields[i]) function and using reflection?


Sorry, it's not at all clear what your question is.

I would suggest, however, that to find a field with a particular name,
you use GetField(name) rather than looping through all the fields.

--
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
Nov 17 '05 #5
Locia <ro********@yahoo.it> wrote:
Sorry It's a assertion not a question.I put a question mark but it's a
error.


What *exactly* is an error?

Sorry, it's still not clear exactly what you're trying to do, or which
bit fails. For instance, the boolean type doesn't *have* any public
instance fields, so you won't get any results from the first line of
code from your first post.

--
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
Nov 17 '05 #6

Jon wrote:
Locia <ro********@yahoo.it> wrote:
Sorry It's a assertion not a question.I put a question mark but it's a
error.
What *exactly* is an error?

What *exactly* is an error?


Why I get different result if I run these two pieces of code?
First result is right.

param.Add("Name");

// first

foreach ( FieldInfo iter in fields)
{
Console.WriteLine(iter.Name);
}
Console.ReadLine();

// second

foreach ( FieldInfo iter in fields)
{
Object obj=Util.GetSubObject(param,iter);
if (obj!=null) Console.WriteLine((String) obj);
}
Console.ReadLine();

Nov 17 '05 #7
Locia <ro********@yahoo.it> wrote:

Jon wrote:
Locia <ro********@yahoo.it> wrote:
Sorry It's a assertion not a question.I put a question mark but it's a
error.
What *exactly* is an error?

What *exactly* is an error?


Why I get different result if I run these two pieces of code?
First result is right.

param.Add("Name");

// first

foreach ( FieldInfo iter in fields)
{
Console.WriteLine(iter.Name);
}
Console.ReadLine();


That's listing the fields in the type in question.
// second

foreach ( FieldInfo iter in fields)
{
Object obj=Util.GetSubObject(param,iter);
if (obj!=null) Console.WriteLine((String) obj);
}
Console.ReadLine();


That's passing in a FieldInfo reference as "currentObj" in
GetSubObject. Now, FieldInfo doesn't have any fields, so it can't find
one with the name "Name".

If you change GetSubObject to iterate through *properties* instead of
fields, it will work (although it would be easier just to call
GetProperty and specify the name).

--
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
Nov 17 '05 #8
> If you change GetSubObject to iterate through *properties* instead of
fields, it will work (although it would be easier just to call
GetProperty and specify the name).


I try to do this but don't work.

What is wrong in my code?

foreach ( FieldInfo iter in fields)
{
PropertyInfo prop=iter.GetType().GetProperty("Name");
Object obj= prop.GetValue(prop,null);
Console.WriteLine((String) obj);
}
Console.ReadLine();

Nov 17 '05 #9
Locia <ro********@yahoo.it> wrote:
If you change GetSubObject to iterate through *properties* instead of
fields, it will work (although it would be easier just to call
GetProperty and specify the name).


I try to do this but don't work.

What is wrong in my code?

foreach ( FieldInfo iter in fields)
{
PropertyInfo prop=iter.GetType().GetProperty("Name");
Object obj= prop.GetValue(prop,null);
Console.WriteLine((String) obj);
}
Console.ReadLine();


You're specifing "prop" as the target, instead of the real target
(iter).

--
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
Nov 17 '05 #10
Thanks Jon, now it work.

Nov 17 '05 #11

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

Similar topics

4
by: Tim Werth | last post by:
I am trying to use reflection to add an event handler for the RowUpdated event of the OracleDataAdapter object (ODP.NET), but the same thing can be said for SqlDataAdapter if you only use...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
2
by: Jason Coyne Gaijin42 | last post by:
I have seen several people looking for a way to access the Columns collection when using the AutoGenerate = true option. Some people have gotten so far as to find the private autoGenColumnsArray...
2
by: Lev | last post by:
Hi, I have some code that does reflection on an assembly I load. When I try to get the attributes on one of the methods implemented in the assembly, the MC++ version does not return anything....
2
by: Marco | last post by:
Hi, I try to use reflection to retrieve class B members and not inherited members. I wonder why the myMember value is nothing. Any suggestions? Thanks Module Module1 Sub Main()
3
by: HL | last post by:
The requirement is to send some information to other objects. The objects to whom the information has to be sent is not available at compile time. The names of the types (objects) will be provided...
7
by: Paul Hadfield | last post by:
Hi, I'm running into one problem with trying to call "Type.GetCustomAttributes(...)" on my reflected code. Basically - I can't trap my own custom attribute - I can only catch / identify system...
5
by: Klaudiusz Bryja | last post by:
Hi, This is for NetCF 2.0. I need to create event handling code which using reflection. I have some parameters in XML which describe how event should be handled. I have code to create...
6
by: Cralis | last post by:
Hi guys, Someone once said, 'You can do that with reflection'. I can't recall what it was I was trying to do at the time, but then he said, 'Any developer knows what reflection is...'. I kept...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.