473,486 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

get all class attributes

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();
}
Jan 18 '08 #1
5 15961
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.

s.******@gmail.com wrote:
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();
}
Jan 18 '08 #2
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.

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

<s.******@gmail.comwrote in message
news:9e**********************************@e23g2000 prf.googlegroups.com...
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();
}

Jan 18 '08 #3
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
Jan 18 '08 #4
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
Jan 18 '08 #5
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" <ma**********@gmail.comwrote in message
news:88**********************************@d4g2000p rg.googlegroups.com...
>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
Jan 18 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

50
6283
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
2
2223
by: Bob Parnes | last post by:
In its default configuration, my version of pylint (0.5.0) sets the maximum number of class attributes at 7. This seems low to me, but I can see how an excessive number might make maintenance more...
0
928
by: harold fellermann | last post by:
Hello, I just posted this question with a wrong subject... So here again with a better one. I am working on a C extension module that implements a bunch of classes. Everything works fine so...
9
2312
by: Sandeep Sharma | last post by:
For many years I have been following the convention of naming all class attributes with a leading underscore. This enables me to quickly identify the class attributes when I encounter them in the...
2
4961
by: Ewald R. de Wit | last post by:
I'm running into a something unexpected for a new-style class that has both a class attribute and __slots__ defined. If the name of the class attribute also exists in __slots__, Python throws an...
1
898
by: dasmart | last post by:
I'm new to the .NET world and am in the process of learning about VB.NET classes. I've tried to find information on class attributes (overview, when/why to use them, etc.) but every link from MSDN...
4
3282
by: Pedro Werneck | last post by:
Hi all I noticed something strange here while explaining decorators to someone. Not any real use code, but I think it's worth mentioning. When I access a class attribute, on a class with a...
2
2128
by: james_027 | last post by:
hi everyone, I am now in chapter 5 of Dive Into Python and I have some question about it. From what I understand in the book is you define class attributes & data attributes like this in python...
4
1599
by: Wilbert Berendsen | last post by:
Hi, is it possible to manipulate class attributes from within a decorator while the class is being defined? I want to register methods with some additional values in a class attribute. But I...
0
833
by: Terry Reedy | last post by:
John Hanks wrote: Newsreaders do not typically have line counters. I presume you are referring to def setSolution(self, val): self.solved = True self.solutions = set((val,))...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7319
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4864
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.