473,722 Members | 2,319 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2526
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 YourClassContai ningTests

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 YourClassContai ningTests

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
4948
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 12:27:49 GMT NNTP-Posting-Host: 24.136.228.213 X-Complaints-To: abuse@earthlink.net X-Trace: newsread3.news.atl.earthlink.net 1088857669 24.136.228.213 (Sat, 03 Jul 2004 05:27:49 PDT) NNTP-Posting-Date: Sat, 03 Jul 2004 05:27:49 PDT Organization:...
3
2303
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". Can this be done and if so how? Thank you Victor
3
1824
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 frm.getAttribute("custom") => returns the value of the attribute
1
2709
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 that the custom control cannot be wired automatically to the calendar control, but i cannot seem to "read" the value of the calendar control via jscript via any other means. Any ideas?
4
1690
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 copies of the files. There are various scripting and "active" technologies enabled on the server: ASP.NET ASP Front Page Extensions Perl
15
2717
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
5242
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
1060
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 "Custom" and set the page size to 9x12 But when I exit that dialog I can't find the values 9 and 12 anywhere in the PrintDocument I passed to the dialog. BUT
8
2812
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);" TextBox2.Attributes.Add("onKeyPress", jscode) You will notice that jscode contains the JavaScript Logical And operator (&&). However, ASP.NET renders this as &amp;&amp; in the code that is
0
9383
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9154
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9088
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8051
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5995
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4762
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3207
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
2
2601
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2147
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.