Reflection Values

  • Thread starter Thread starter Roger Webb
  • Start date Start date
R

Roger Webb

Hey All,

I've got a class that I'm trying to get a listing of the properties...kinda
like one of the Reflection examples from Microsoft. However, unlike their
example, I need to get the value as well. And am having trouble
understanding how the GetValue() function of the PropertyInfo should be
setup as below.

Also, with it coming back as an object, is there a way to get it recast so
that the DoSomething function will work?


I'm hopeing/thinking/praying.. that there is a way to make the code below
have StringBuilder1.ToString() be
"-FirstField='String1'-SecondField=2"

Any help ... or comments would be appreciated.

- Roger


class LocalClass
{
public string FirstField;
public int SecondField;
}

<snip most of procedure>
....
LocalClass LocalClass1 = new LocalClass();
LocalClass1.FirstField = "String1";
LocalClass1.SecondField = 2;

Type DRType = LocalClass1.GetType();
PropertyInfo[] PropInfo = DRType.GetProperties();
foreach(PropertyInfo prop in PropInfo)
{
if(!(prop.PropertyType.IsArray))
{
StringBuilder1.Append("-"+DoSomething(prop.Name,
prop.GetValue(prop, ????)));
}
}
....


public string DoSomething(string A, string B)
{
return string.Format("{0}='{1}'",A,B);
}
public string DoSomething(string A, int B)
{
return string.Format("{0}={1}",A,B.ToString());
}
 
Roger Webb said:
I've got a class that I'm trying to get a listing of the properties...kinda
like one of the Reflection examples from Microsoft. However, unlike their
example, I need to get the value as well. And am having trouble
understanding how the GetValue() function of the PropertyInfo should be
setup as below.

The call to prop.GetValue should be

prop.GetValue (LocalClass1, null);

However, your class LocalClass doesn't actually have any properties, as
you've written it - it has two fields instead.
Also, with it coming back as an object, is there a way to get it recast so
that the DoSomething function will work?

Yes - cast it to string.
 
The call to prop.GetValue should be
prop.GetValue (LocalClass1, null);

That was it.
However, your class LocalClass doesn't actually have any properties, as
you've written it - it has two fields instead.

Yeah.. That was because it was a quick example... Didnt create it exactly
right. My Bad. But, Thanks... I was missing the concept the the First
Parameter was the class that the property belonged to.
Yes - cast it to string.

I can believe that, however How can you cast it as .. whatever is was... for
example...

DoSomething(prop.Name, (???) prop.GetValue(LocalClass1, null)));

What would you put here ... that would be "String" in the first case hence
casting the Property as a String.... and an "int" in the second case,
casting that property as an int... so that it will use the appropriate
overloaded method.

Filling that casting spot with prop. something give a compile error.

- Roger
 
Roger,

You can't do it. You would need to have a large switch statement based
on type (or the name of the type, rather), which would output the values you
want based on type, or, you could just call the ToString method on the
object, and it will return something (the value, in some cases, otherwise,
usually just the type name).

Hope this helps.
 
Roger Webb said:
That was it.
Goodo.


I can believe that, however How can you cast it as .. whatever is was... for
example...

DoSomething(prop.Name, (???) prop.GetValue(LocalClass1, null)));

Ah, you can't do that.

You'll need to know ahead of time which overload to use... *or*
possibly use reflection to find the right method and invoke it as well.
 
Back
Top