Accessing Properties on an object with special attributes

T

Thorsten Viel

Hi,

after searching and testing for hours, im off with that.

Maybe you have a snippet or link for me.


I have a class with properties. Some of the properties (not all) have a
custom attribute. Now i want to call a method which prints out the values
of the properties with the custom attributes, and only these values.

Howto find out which Properties (Name) have those attributes (Reflection) is
ok, but how would i access a concrete existing object and get the value of
a property, where i have only the reflected name from.


Thanks in advance

Thorsten
 
A

allfyre

Get a System.Reflection.PropertyInfo object for the properties you want
to manipulate. Then use the PropertyInfo.GetValue and
PropertyInfo.SetValue methods.

I'm sure a quick search for "PropertyInfo.SetValue" will point you to
what you're looking for.
 
A

allfyre

....You would pass the instance of the object from which you wanted to
read values to the GetValue method.
 
J

Jon Skeet [C# MVP]

Thorsten Viel said:
after searching and testing for hours, im off with that.

Maybe you have a snippet or link for me.


I have a class with properties. Some of the properties (not all) have a
custom attribute. Now i want to call a method which prints out the values
of the properties with the custom attributes, and only these values.

Howto find out which Properties (Name) have those attributes (Reflection) is
ok, but how would i access a concrete existing object and get the value of
a property, where i have only the reflected name from.

Use PropertyInfo.GetValue. Chances are you'll already have the
PropertyInfo from the way you've found the properties with the
appropriate attribute.
 
T

Thorsten Viel

allfyre said:
...You would pass the instance of the object from which you wanted to
read values to the GetValue method.


Works lika a charm. Thanks to all of you.
 

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