get all class attributes

S

s.luongo

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

Bob Powell [MVP]

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.


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

Nicholas Paldino [.NET/C# MVP]

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

Marc Gravell

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
 
M

Marc Gravell

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
 
M

Marc Gravell

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
 
B

Bob Powell [MVP]

You're right about the detail. Posting while tired ;-)

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


Marc Gravell said:
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
 

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