471,337 Members | 862 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

declarative prograaming (?) in C#

I keep coming accross C# code that has sections of code like this:

[System.Diagnostics.ConditionalAttribute]

could any one please explain what these sections are (reminds me of
XDoclet tags in Java programming) or better still - a reference to where
I can read up on this.

Thanks

Dec 3 '05 #1
4 1496
Ann,
could any one please explain what these sections are (reminds me of
XDoclet tags in Java programming) or better still - a reference to where
I can read up on this.


They are called (custom) attributes. You can read more here

http://msdn.microsoft.com/library/en...attributes.asp
http://msdn.microsoft.com/library/en...Attributes.asp
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 3 '05 #2
Ann Huxtable <an**********@research-labs.de> wrote:
I keep coming accross C# code that has sections of code like this:

[System.Diagnostics.ConditionalAttribute]

could any one please explain what these sections are (reminds me of
XDoclet tags in Java programming) or better still - a reference to where
I can read up on this.


They're attributes - a bit like XDoclet, yes, very similar to the
annotations of Java 1.5.

What exactly each attribute means is up to the attribute itself - look
the attribute up in MSDN.

Unfortunately, MSDN doesn't seem to have a very good overview of
attributes in general (at least, not that I can find). I suggest you
search for C# and attributes on Google.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 3 '05 #3
Attributes enable you to add metadata to the compiled assembly which can be
used at runtime by the reflection API's.

There is a section in MSDN entitled "Attributed Programming"

--
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.

"Ann Huxtable" <an**********@research-labs.de> wrote in message
news:dm**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
I keep coming accross C# code that has sections of code like this:

[System.Diagnostics.ConditionalAttribute]

could any one please explain what these sections are (reminds me of
XDoclet tags in Java programming) or better still - a reference to where I
can read up on this.

Thanks

Dec 3 '05 #4
Ann Huxtable wrote:
I keep coming accross C# code that has sections of code like this:

[System.Diagnostics.ConditionalAttribute]


Just to add to what the others have said.

Metadata is information that describes .NET types. There is runtime
metadata that the compiler *must* generate, and there is custom metadata
that the developer can add. Now it gets complicated and Microsoft's
documentation does not help. .NET languages add metadata through
attributes, however, both custom *and* runtime metadata can be added
using the same mechanism.

For example, the attribute you give, [Conditional] (you can omit the
Attribute suffix) is a *custom* attribute, it tells the compiler to add
custom metadata to the assembly. This custom metadata is read by a
compiler when it compiles code that will use the library, and it
indicates that a call to the code marked with this attribute can only be
made if a particular compiler symbol is defined. The [Conditional]
custom metadata is added to the library that has this attribute, but the
runtime will ignore this metadata. These are known as _custom
attributes_. You can use Type.GetCustomAttributes (or
Attribute.GetCustomAttribute) to get access to this metadata.

Another attribute, [Serializable] does not add custom metadata, instead
it is a message to the compiler to change the runtime metadata that it
will generate (in this case, set the runtime metadata on a class to show
that it is serializable). Another example is [AssemblyVersion] that
tells the compiler to set the assembly metadata to the specified
version. There is no custom metadata added to the assembly, so you
*cannot* access this information through Type.GetCustomAttributes.
(There are other reflection methods/properties to get this information.)
These are known as _pseudo custom attributes_.

In general, the runtime will ignore the metadata from custom attributes,
but it will act upon the metadata from pseudo custom attributes.
However, there are some custom attributes that the runtime will pay
attention to. An example is [OneWay] which indicates that a method
should be called in a fire-and-forget fashion (that is, it does not
return a value) this adds custom metadata to the method, but the runtime
will specifically look for this custom metadata and act upon it. You can
read this custom metadata using Type.GetCustomAttributes. These
attributes are called _distinguished custom attributes_.

So there are three types of 'custom attributes', but what is so special
about serialization? I don't know. I don't know why the serializable
runtime metadata has to be specified with a pseudo custom attribute and
not with a keyword. For example, the visibility of a type (public,
private, internal, yet another piece of metadata) is not specified with
a pseudo custom attribute it is specified with a keyword.

This is the accepted syntax:

[Serializable]
public class MyData
{
}

However, since public tells the compiler to generate metadata the C#
language designers could have been more consistent and used the
following (this is not code that will compile with the current
compilers):

[Serializable]
[Public]
class MyData
{
}

or (more readable)

[Serializable, Public]
class MyData
{
}

IMO the C# language designers have been inconsitent. Their use of custom
attributes is a mess. But then again, they never listen to my
opinions...

As you can see the three types of attributes are very different in how
they are implemented and what code uses them. Sadly the .NET
documentation does not make this obvious.

Richard
--
http://www.grimes.demon.co.uk/workshops/fusionWS.htm
http://www.grimes.demon.co.uk/workshops/securityWS.htm
Dec 4 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Christoph Wienands | last post: by
3 posts views Thread by Ken Cox [Microsoft MVP] | last post: by
1 post views Thread by drop | last post: by
20 posts views Thread by Artur Siekielski | last post: by

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.