473,401 Members | 2,146 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,401 software developers and data experts.

PP: Using "custom attributes"

Hi Folks

I was reading this article
(http://www.dotnetbips.com/articles/d...cle.aspx?id=32) on
"Custom Attribute", written by Bipin.

The only thing I did not understand in this article is the usage of
"Custom Attribute" in real life project. Can somebody please help me
understand where are these informations really helpful in Development
Environment; may be a few example(s) will help me understand.

TIA
PP

Jun 5 '06 #1
2 2498
First of all, do you know reflection well? Reflection is a mechanism that
allows runtime inspection of your classes, their members and methods etc.

Reflection typically comes in handy when you need to handle unknown classes
and assemblies (i.e. classes and assemblies over which you have no control)
in a generic way.

A few samples where reflection is useful.
http://msdn.microsoft.com/msdnmag/is...n/default.aspx
http://msdn.microsoft.com/msdnmag/is...s/default.aspx

Personally, I like to view attributes as an extension to the reflection
concept. While reflection traditionally only deals with the implicit
description of your code (classes, members, methods etc), attributes gives
you the possibility to supplement the implicit descriptions of your code
with explicit information.

Attributes only make sense in conjunction with one or more features. A
particular set of attributes can be viewed as "the configuration of your
class (assembly, method, etc...) for a feature or set of features". I am
sure you have noticed that the .NET framwork uses attributes rather
extensively for features such as controlling design-time behavior,
marshaling, serialization and security. Don't you think using declarative
security with attributes is a convenient way to take advantage of .NET
framework security features? You surely wouldn't want to swap that with
manual coding, would you?
Another more precise example is the Obsolete attribute which you may use to
signal that a method will be removed in a future version of your code. Then
there is a feature in the compiler that will look out for this obsolete
attribute and will provide a warning that you are using an obsolete method.
There is even another attribute that allows you to define that the compiler
should treat such a case as an error (broken build).

So I think understanding how and why the standard framework features are
utilizing .NET standard attributes, and why it is useful, is a good starting
point for understanding where custom attributes might help you.

To give some concrete examples I would, like with reflection, turn to
generic tool and frameworks :
1)NUnit uses attributes, such as

[TestFixture]
public class YourClassContainingTests

to "mark" your test classes such that the NUnit runtime can figure out what
tests to run. Notice that the same could have been achieved with reflection
(such as JUnit for Java does), but attributes is simpler, clearer and less
code intrusive (because an attribute-based approach can rely on explicit
information about code, while a pure reflection-based model must define
convention (test class must be a subclass of TestCase and test methods must
start with "test" etc..)).

2)AspectDNG AOP Framework
You may not be familiar with AOP, but think of it as follows:
-You have your "code", to which you want to add features (In AOP, features
are called aspects). A typical aspect could be logging.
-You code the features according to your AOP framework (independently of
your "code)"
-You apply the features to the "code" by specifying which aspect should be
applied to what "code", either at runtime (dynamics AOP) or during an extra
build step (static AOP).

AspectDNG uses static AOP, were the magic happens during the extra build
steps to "weave" aspects into the "code". If a logging aspect is applied to
some "code", the product of the extra build step is code with logging. With
static AOP, there is nothing AOP'ish about the code at runtime.

So what has this AspectDNG thing to do with custom attributes?
Well, the thing is AspectDNG implements this extra build step through a tool
that uses custom attributes. It accepts a "code" assembly and an aspect
assembly as input. The aspects in the aspect assembly are marked with custom
attributes to specify where the aspects should apply. The tool inspects
these attributes and emits the "final code". For our example with a logging
aspect, the "final code" emitted by the tool would be logging code inserted
alongside your "code".

Take a look at http://www.dotnetguru.org/sarl/aspectdng/ for more details
about aspectdng as well as its custom attributes.

This became a lot more lengthy than I planned, but I hope it helps! :-)

Tor Bådshaug
tor.badshaug [//at\\] bekk.no.
Jun 5 '06 #2
Hi

I am analzing the AspectDNG, i would like to weave my custom attribute,
please provide me an example for the same.

Thanks in advance
Ravi

Tor Bådshaug wrote:
First of all, do you know reflection well? Reflection is a mechanism that
allows runtime inspection of your classes, their members and methods etc.

Reflection typically comes in handy when you need to handle unknown classes
and assemblies (i.e. classes and assemblies over which you have no control)
in a generic way.

A few samples where reflection is useful.
http://msdn.microsoft.com/msdnmag/is...n/default.aspx
http://msdn.microsoft.com/msdnmag/is...s/default.aspx

Personally, I like to view attributes as an extension to the reflection
concept. While reflection traditionally only deals with the implicit
description of your code (classes, members, methods etc), attributes gives
you the possibility to supplement the implicit descriptions of your code
with explicit information.

Attributes only make sense in conjunction with one or more features. A
particular set of attributes can be viewed as "the configuration of your
class (assembly, method, etc...) for a feature or set of features". I am
sure you have noticed that the .NET framwork uses attributes rather
extensively for features such as controlling design-time behavior,
marshaling, serialization and security. Don't you think using declarative
security with attributes is a convenient way to take advantage of .NET
framework security features? You surely wouldn't want to swap that with
manual coding, would you?
Another more precise example is the Obsolete attribute which you may use to
signal that a method will be removed in a future version of your code. Then
there is a feature in the compiler that will look out for this obsolete
attribute and will provide a warning that you are using an obsolete method.
There is even another attribute that allows you to define that the compiler
should treat such a case as an error (broken build).

So I think understanding how and why the standard framework features are
utilizing .NET standard attributes, and why it is useful, is a good starting
point for understanding where custom attributes might help you.

To give some concrete examples I would, like with reflection, turn to
generic tool and frameworks :
1)NUnit uses attributes, such as

[TestFixture]
public class YourClassContainingTests

to "mark" your test classes such that the NUnit runtime can figure out what
tests to run. Notice that the same could have been achieved with reflection
(such as JUnit for Java does), but attributes is simpler, clearer and less
code intrusive (because an attribute-based approach can rely on explicit
information about code, while a pure reflection-based model must define
convention (test class must be a subclass of TestCase and test methods must
start with "test" etc..)).

2)AspectDNG AOP Framework
You may not be familiar with AOP, but think of it as follows:
-You have your "code", to which you want to add features (In AOP, features
are called aspects). A typical aspect could be logging.
-You code the features according to your AOP framework (independently of
your "code)"
-You apply the features to the "code" by specifying which aspect should be
applied to what "code", either at runtime (dynamics AOP) or during an extra
build step (static AOP).

AspectDNG uses static AOP, were the magic happens during the extra build
steps to "weave" aspects into the "code". If a logging aspect is applied to
some "code", the product of the extra build step is code with logging. With
static AOP, there is nothing AOP'ish about the code at runtime.

So what has this AspectDNG thing to do with custom attributes?
Well, the thing is AspectDNG implements this extra build step through a tool
that uses custom attributes. It accepts a "code" assembly and an aspect
assembly as input. The aspects in the aspect assembly are marked with custom
attributes to specify where the aspects should apply. The tool inspects
these attributes and emits the "final code". For our example with a logging
aspect, the "final code" emitted by the tool would be logging code inserted
alongside your "code".

Take a look at http://www.dotnetguru.org/sarl/aspectdng/ for more details
about aspectdng as well as its custom attributes.

This became a lot more lengthy than I planned, but I hope it helps! :-)

Tor Bådshaug
tor.badshaug [//at\\] bekk.no.


Jun 23 '06 #3

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

Similar topics

0
by: Doug | last post by:
Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Lines: 14 Message-ID: <9DxFc.23833$bs4.23734@newsread3.news.atl.earthlink.net> Date: Sat, 03 Jul 2004...
3
by: Victor | last post by:
Hi, I have some sample XML and an XSD below I have written. The XSD almost does what I want. What I need is some way of enforcing that AT LEAST TWO of the attributes "TestAttribute" are "X". ...
3
by: F. Da Costa | last post by:
Hi, I was wondering *why* there is a difference between the results of the following two statements. On the suface they seem to do the same (or do they?) frm => returns void ...
1
by: charliewest | last post by:
Is it possible to use the custom validation control to validate a calendar control via client-side script (in jscript)? I'm familiar with custom validation and client side script, and i am aware...
4
by: Dotcom | last post by:
I have an ASP.NET application that is mysteriously acquiring height and width attributes on a particular IMG element on multiple pages. This is not being caused by someone editing or uploading new...
15
by: bill salkin | last post by:
I'd like to create a custom error handler like this in VB.NET: .... try ... Throw ("Lender Name not in table") .... catch ("Lender Name not in table")
2
by: Daren Hawes | last post by:
Hi I need to add an attribute to a Textbox to make it read only. To add a CSS I use DeptDate.Attributes("Class") = "textInput" That adds 'Class="textinput"', but the readonly is like..
0
by: Just Me | last post by:
I suppose I'd have to be extremely lucky to find someone who knows about what I now write but I have to try. Using PageSetupDialog and then clicking Printer and then Properties I can select...
8
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);"...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.