473,398 Members | 2,525 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,398 software developers and data experts.

Reflection Values

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


Nov 16 '05 #1
4 2320
Roger Webb <rw***@nos.twia.org> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
> 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.
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.


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
Nov 16 '05 #3
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Roger Webb" <rw***@nos.twia.org> wrote in message
news:uV*************@TK2MSFTNGP14.phx.gbl...
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.
> 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.


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

Nov 16 '05 #4
Roger Webb <rw***@nos.twia.org> wrote:
The call to prop.GetValue should be

prop.GetValue (LocalClass1, null);


That was it.


Goodo.
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.


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

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

Similar topics

2
by: ichor | last post by:
hi i have read a few examples that use the System.Reflection api , but i cant understand in what way is it helpful. i mean its practical use in the real world thanx
5
by: Frazer | last post by:
hi could any one tell me which real life senarios reflection can be used in ? thnx
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...
1
by: Mike Malter | last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I can call that method again later using reflection. I am...
7
by: John | last post by:
I have a class the reads in a file and sets the values of the file into its properties. This class is used to populate the data onto a form. This form has controls created at runtime based on...
2
by: V. Jenks | last post by:
I am using reflection to load classes and I can't find any examples on how to pass values to constructor parameters using reflection, how is this done? Here's an example of one of my methods, as...
0
by: Cat | last post by:
I have class Base, and class Derived. I am serializing Derived objects from XML, and these Derived objects are allowed to 'inherit' the values from other named Base objects already defined in...
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...
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...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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 project—planning, coding, testing,...
0
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...

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.