reflection problem

L

Locia

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;
}
 
J

Jon Skeet [C# MVP]

Locia said:
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.
 
L

Locia

I would like do it
for (int i=0;i<fields.Length;i++)
Console.WriteLine(fields.Name);

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

Jon Skeet [C# MVP]

Locia said:
I would like do it
for (int i=0;i<fields.Length;i++)
Console.WriteLine(fields.Name);

but rather use fields.Name I must access to fields.Name with
GetSubObject("Name",fields) 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.
 
J

Jon Skeet [C# MVP]

Locia said:
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.
 
L

Locia

Jon said:
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();
 
J

Jon Skeet [C# MVP]

Locia said:
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).
 
L

Locia

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();
 
J

Jon Skeet [C# MVP]

Locia said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top