473,425 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,425 software developers and data experts.

Example Of an Attribute

Hi All,

I would like to know where & when exactly we would have a
need for attributes. A realistic example.
Thanz in Advance,
Justine
Nov 15 '05 #1
4 4597
One example is their use by the XmlSerializer component. The XmlSerializer
reads the public data members of an arbitrary class instances, and
serializes them as XML. By placing specific attributes on the data members,
you can control how the XmlSerializer serializes each individual member.
For example, the XmlSerializer will pick a default element name based on the
name of the data member - but you can put the XmlElementAttribute attribute
on a data member, and set the ElementName property of that attribute, and
change the name of the XML element which gets written for that data member.

--Don

--
This posting is provided "AS IS" with no warranties, and confers no rights.

"Justine" <am***********@hotmail.com> wrote in message
news:00****************************@phx.gbl...
Hi All,

I would like to know where & when exactly we would have a
need for attributes. A realistic example.
Thanz in Advance,
Justine

Nov 15 '05 #2
An attribute enables you to add some information, or metadata, to classes,
properties, methods or events at compile time which can be used by the
reflection system at runtime.

A good example of this is the BrowsableAttribute which can be added to a
property and which controls the visibility of properties for the property
grid. When an object is passed to the property grid's SelectedObject
property, the grid uses reflection to get a property descriptor for each of
the public properties the object provides, it filters these property
descriptors, only keeping the ones with the [Browsable(true)] attribute and
uses their names, property types and values to populate the grid.

The property grid also uses the EditorAttribute to determine how a value
might be edited, for example, the Color structure has had an editor
attribute applied that informs the property grid that Color structures will
be edited with the colour UITypeEditor.

You can create your own attributes too that can be used to provide custom
information for classes, methods, properties and so-on. The attributes for a
particular thing may be discovered by using reflection. the TypeDescriptor
class enables you to get a collection of property descriptors for a class.
Each property descriptor has an Attributes collection that contains all of
the attributes applied to each property.

Because attributes add data to something at compile-time, you cannot modify
them at runtime. This means that for example, you cannot do something like
(pseudocode)
TypeDescriptor.GetProperty("fred").Attributes["Broswsable"]=true;
Hope this unmuddies the waters a bit...

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

The November edition of Well Formed is now available.
Learn how to create Shell Extensions in managed code.
http://www.bobpowell.net/currentissue.htm

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

Read my Blog at http://bobpowelldotnet.blogspot.com

"Justine" <am***********@hotmail.com> wrote in message
news:00****************************@phx.gbl...
Hi All,

I would like to know where & when exactly we would have a
need for attributes. A realistic example.
Thanz in Advance,
Justine

Nov 15 '05 #3
We've used custom attributes on object properties that we want to
persist when we write our objects out to disk. Using Reflection, we
just loop over the properties for the object, check to see if this
attribute is set and, if it is, write that property name and it's
value out to a file.
Additionally I've used a custom attribute to provide a "pretty name"
to properties that are displayed in a PropertyGrid. Normally, it just
displays the property name and requires all of the same restrictions
(primarily, no spaces). Using my attribute, I could specify an
arbitrary string to display as the property name.

Those are just 2 examples of how we've used them in our code.
"Justine" <am***********@hotmail.com> wrote in message news:<00****************************@phx.gbl>...
Hi All,

I would like to know where & when exactly we would have a
need for attributes. A realistic example.
Thanz in Advance,
Justine

Nov 15 '05 #4
Thanz a lot all of u :-)
-----Original Message-----
Hi All,

I would like to know where & when exactly we would have aneed for attributes. A realistic example.
Thanz in Advance,
Justine
.

Nov 15 '05 #5

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

Similar topics

6
by: Patrick K. O'Brien | last post by:
I'm looking for a good schema to use as an example for an object database system. Something like a books/authors/publishers or teachers/students/courses kind of thing. There are plenty of...
7
by: Porky Pig Jr | last post by:
Hello, I"m still learning Python, but going through the Ch 5 OOP of Nutshell book. There is discussion on __slots__, and my understanding from reading this section is that if I have a class...
7
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. #...
3
by: srijit | last post by:
Hello, Here is an example code of xml writer in Python+PythonNet, Ironpython and Boo. The codes look very similar. Regards, Srijit Python + PythonNet: import CLR
4
by: erick_bodine | last post by:
In Martinelli's Nutshell book in the Exceptions chapter there is an example of a custom exception class (pg.112) that I am trying to implement without success. The custom exception class example...
23
by: Alberto | last post by:
An OUTSTANDING example of a rebuttal. Worth a glance. Rather long, OBVIOUSLY feel fee to dismiss it entirely if not interested. But this habit of the rebuttals must go to a stop (forgive odd...
2
by: kathy | last post by:
Could anyone know where I can find example to write a COM component / ActiveX control in C# which can be used by VB6/VC6?
1
by: prem | last post by:
Hi , Any one please give me the Usage with example for AutoEventWireUp PageDirective. Regards, Prem.
1
by: gimme_this_gimme_that | last post by:
This is a clip from the new book Learning the Yahoo! User Interface Library ... (An example of doing things and not using YUI) Why does this return a NS_ERROR_DOM_HIERARCHY_REQUEST _ERR code 3...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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
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
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
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
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.