Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

get all class attributes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
s.luongo@gmail.com - 18 Jan 2008 18:17 GMT
Hello everybody,

Trying to get all attributes from a class (class1), I got them through
properties, is it a simple way to get all class atributes not using
their properties??

public void GetAllClassAttributes()
        {
            Type classType = class1.GetType();
            PropertyInfo[] a_pi= classType.GetProperties();
            foreach ( PropertyInfo pi in a_pi)
            {
                if ( pi.CanRead )                                pi.GetValue(resumen, null).ToString();
            }
            Console.Read();
        }
Bob Powell [MVP] - 18 Jan 2008 18:28 GMT
You need to make the distinction between attributes applied to the class
and those applied to the class members.

For a class, or more properly a type, you can use GetType().Attributes.

For other members you would go via the TypeDescriptor to obtain the
MemberInfo objects for the members you were interested in such as
properties, fields and methods.

Signature

Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

> Hello everybody,
>
[quoted text clipped - 12 lines]
>             Console.Read();
>         }
Marc Gravell - 18 Jan 2008 20:44 GMT
> For other members you would go via the TypeDescriptor to obtain the
> MemberInfo objects for the members you were interested in such as
> properties, fields and methods.

Unless I missed it, TypeDescriptor doesn't really yield MemberInfo
instances - that is the reflection world. You can get at
PropertyDescriptors and EventDescriptors, but not fields (since
component-model [=TypeDescriptor] isn't implementation-specific, but
fields are /usually/ an implementation detail).

To get at MemberInfo (including FieldInfo) you would use the Type
directly.

Marc
Bob Powell [MVP] - 18 Jan 2008 22:08 GMT
You're right about the detail. Posting while tired ;-)

Signature

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

>> For other members you would go via the TypeDescriptor to obtain the
>> MemberInfo objects for the members you were interested in such as
[quoted text clipped - 10 lines]
>
> Marc
Nicholas Paldino [.NET/C# MVP] - 18 Jan 2008 18:34 GMT
You are getting the property values, not attributes here.  If you want
the attributes on the class, then you need to call the GetCustomAttributes
method on the Type instance for that method.

   If you are asking how to get all the values on all the properties
exposed by a type, then what you are doing is the best way.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Hello everybody,
>
[quoted text clipped - 12 lines]
> Console.Read();
> }
Marc Gravell - 18 Jan 2008 20:33 GMT
> If you are asking how to get all the values on all the properties
> exposed by a type, then what you are doing is the best way.

Without trying to labor the point, I'd say that it is still definitely
a two-horse race between reflection and component-model; with
component-model you get access to dynamic properties (such as the
columns on a DataView, or the extra [contextual] properties that
appear in the Visual Studio property window) - plus it can be several
orders of magnitude quicker than reflection if you need it to be (but
you can't speed up reflection).

Some similar code using component-model (not quite sure if it does
what the OP's does, since it doesn't output anything...)

           object obj = //whatever
           foreach (PropertyDescriptor pd in
TypeDescriptor.GetProperties(obj))
           {
               Console.WriteLine(pd.GetValue(obj));
           }

But that's just my tuppence...

Marc
Marc Gravell - 18 Jan 2008 20:37 GMT
> Trying to get all attributes from a class (class1), I got them through
> properties, is it a simple way to get all class atributes not using
> their properties??
> ...
> Type classType = class1.GetType();

I really think that the right terminology would help here... first
"class1" appears to be an object, not a class - and you appear to be
obtaing the values of instance properties, nothing to do with
attributes. It also isn't clear whether "class1" and "resumen" are the
same thing, or what the code is intended to do (since it doesn't
output anything).

This is not intended as criticism; just so that you know the right
terms etc in case you want to clarify what you mean.

Marc

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.