Accessing Properties on an object with special attributes

  • Thread starter Thread starter Thorsten Viel
  • Start date Start date
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
 
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.
 
....You would pass the instance of the object from which you wanted to
read values to the GetValue method.
 
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.
 
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.
 
Back
Top