473,549 Members | 2,573 Online
Bytes | Software Development & Data Engineering Community
+ 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 GetAllClassAttr ibutes()
{
Type classType = class1.GetType( );
PropertyInfo[] a_pi= classType.GetPr operties();
foreach ( PropertyInfo pi in a_pi)
{
if ( pi.CanRead ) pi.GetValue(res umen, null).ToString( );
}
Console.Read();
}
Jan 18 '08 #1
5 15967
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().Attri butes.

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 GetAllClassAttr ibutes()
{
Type classType = class1.GetType( );
PropertyInfo[] a_pi= classType.GetPr operties();
foreach ( PropertyInfo pi in a_pi)
{
if ( pi.CanRead ) pi.GetValue(res umen, 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 GetCustomAttrib utes
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.co m

<s.******@gmail .comwrote in message
news:9e******** *************** ***********@e23 g2000prf.google groups.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 GetAllClassAttr ibutes()
{
Type classType = class1.GetType( );
PropertyInfo[] a_pi= classType.GetPr operties();
foreach ( PropertyInfo pi in a_pi)
{
if ( pi.CanRead ) pi.GetValue(res umen, 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
PropertyDescrip tors and EventDescriptor s, 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**********@g mail.comwrote in message
news:88******** *************** ***********@d4g 2000prg.googleg roups.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
PropertyDescrip tors and EventDescriptor s, 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
6306
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 def foo(self, data): self.attr1=data self.attr2.append(data) The initialization of attr1 is obviously OK, all instances of Father redefine it in the...
2
2228
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 difficult. Is this indeed the best value for a maximum under ordinary conditions? If not, can anyone suggest a more reasonable value? Thanks in...
0
935
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 far, but I cannot find any way to implement class attributes or inner classes. Consider you have the following lines of Python :
9
2324
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 source code. Even the GoF book follows this convention, although it's true that the GoF book is not an authoritative source for coding guidelines....
2
4968
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 AttributeError. Is this by design (if so, why)? class A( object ): __slots__ = ( 'value', ) value = 1 def __init__( self, value = None ):
1
903
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 says "page not found". Can anyone point me to basic information on class attributes or is there some reason why it's not on MSDN?
4
3295
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 custom metaclass with a __getattribute__ method, the method is used when acessing some attribute directly with the class object, but not when you do...
2
2133
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 class Book: total # is a class attribute
4
1605
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 can't get a decorator to change a class attribute while the class is still being defined. Something like: class Parser(object): regexps =
0
835
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,)) self.matrix.changed = True for other in self.row+self.col+self.submatrix: if other is self: continue
0
7726
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7967
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7485
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5377
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5097
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3505
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3488
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1953
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
772
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.