PC Review


Reply
Thread Tools Rate Thread

Accessing Properties on an object with special attributes

 
 
Thorsten Viel
Guest
Posts: n/a
 
      18th Jul 2006
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



 
Reply With Quote
 
 
 
 
allfyre
Guest
Posts: n/a
 
      18th Jul 2006
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.

 
Reply With Quote
 
allfyre
Guest
Posts: n/a
 
      18th Jul 2006
....You would pass the instance of the object from which you wanted to
read values to the GetValue method.

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      18th Jul 2006
Thorsten Viel <(E-Mail Removed)> wrote:
> 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.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Thorsten Viel
Guest
Posts: n/a
 
      18th Jul 2006
allfyre wrote:

> ...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.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Accessing object properties Kevin Microsoft C# .NET 1 10th Oct 2008 06:54 PM
Accessing a Parent Object's Properties RSH Microsoft VB .NET 8 9th Mar 2007 07:02 PM
Arraylist - accessing object properties? Alex Microsoft C# .NET 3 20th Oct 2006 08:58 PM
RE: Accessing all properties of an object at Runtime Yan-Hong Huang[MSFT] Microsoft ASP .NET 0 7th Jul 2003 08:13 AM
Re: Accessing all properties of an object at Runtime Ilyas Microsoft ASP .NET 1 4th Jul 2003 06:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:09 PM.