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

Attribute

Hello!

Here I have a simple example of a custom defined Attribute class called
CodeStatus.
Now to my question when I debug this application and I set a breakpoint in
the CodeStatus attribute class
I will never enter that code. So what is the point in using attribute when
the Attribute class is never executed.

I have probably misunderstood something here about Attribute.

So when is the attribute class CodeStatus called ?
Why is it not possible to set a breakpoint that will be hit when the
Attribute class is executed.
using System;

public class CodeStatus : System.Attribute
{
private string STATUS;
private string TESTER;
private string CODER;

public CodeStatus(string Status)
{
this.STATUS = Status;
}

public string Tester
{
set { TESTER = value; }
get { return TESTER; }
}

public string Coder
{
set { CODER = value; }
get { return CODER; }
}

public override string ToString()
{ return STATUS; }
}

[CodeStatus("Final", Coder="Bills")]
public class Rectangle
{
private int length;
private int height;

public Rectangle(int _length, int _height)
{
length = _length;
height = _height;
}

public int Length
{
set { length = value; }
get { return length; }
}

public int Height
{
set { height = value; }
get { return height; }
}

}

public class myApp
{
static void Main(string[] args)
{
Rectangle rect = new Rectangle(5,7);
}
}

//Tony

Oct 8 '08 #1
6 2190
Tony Johansson <jo*****************@telia.comwrote:
Here I have a simple example of a custom defined Attribute class called
CodeStatus.
Now to my question when I debug this application and I set a breakpoint in
the CodeStatus attribute class
I will never enter that code. So what is the point in using attribute when
the Attribute class is never executed.
The point is to store metadata. If you fetch the attribute on the
Rectangle class, you'll be able to access the properties. I don't know
exactly how attributes are serialized/deserialized, but it shouldn't
really matter - they're not expected to be "smart" in themselves.
They're just a way of decorating elements of your code with metadata.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 8 '08 #2
Hello!

I just want to know when is the custom attibute CodeStatus called ?
Do you mean that it's called in the background in some way that's way I
can't use the debugger.

Even if I can't use the debugger the CodeStatus attribute is loaded with
data from
CodeStatus("Final", Coder="Bills")]

Does it sound correct what I'm saying.

//Tony

"Jon Skeet [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:MP*********************@msnews.microsoft.com. ..
Tony Johansson <jo*****************@telia.comwrote:
>Here I have a simple example of a custom defined Attribute class called
CodeStatus.
Now to my question when I debug this application and I set a breakpoint
in
the CodeStatus attribute class
I will never enter that code. So what is the point in using attribute
when
the Attribute class is never executed.

The point is to store metadata. If you fetch the attribute on the
Rectangle class, you'll be able to access the properties. I don't know
exactly how attributes are serialized/deserialized, but it shouldn't
really matter - they're not expected to be "smart" in themselves.
They're just a way of decorating elements of your code with metadata.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

Oct 8 '08 #3
Tony Johansson <jo*****************@telia.comwrote:
I just want to know when is the custom attibute CodeStatus called ?
What do you mean by "called"? It's a class - classes aren't "called",
methods are called.
Do you mean that it's called in the background in some way that's way I
can't use the debugger.

Even if I can't use the debugger the CodeStatus attribute is loaded with
data from
CodeStatus("Final", Coder="Bills")]

Does it sound correct what I'm saying.
I suspect it's hidden by the magic of how the CLR
serializes/deserializes attributes. I've never tried putting any non-
trivial code in attribute properties.

The may be more in CLR via C#, which I'm reading at the moment... I'll
let you know if I find anything.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 8 '08 #4
Hello!

What I mean is from somewhere is the property and methods for the CodeStatus
called and
I can use reflection to ask for example the Rectangle class about all
attributes that is target that class.

When I do so I can see that the custom attribute class has some data that
was set in some way by this
CodeStatus("Final", Coder="Bills")]
statement.

//Tony
"Jon Skeet [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:MP*********************@msnews.microsoft.com. ..
Tony Johansson <jo*****************@telia.comwrote:
>I just want to know when is the custom attibute CodeStatus called ?

What do you mean by "called"? It's a class - classes aren't "called",
methods are called.
>Do you mean that it's called in the background in some way that's way I
can't use the debugger.

Even if I can't use the debugger the CodeStatus attribute is loaded with
data from
CodeStatus("Final", Coder="Bills")]

Does it sound correct what I'm saying.

I suspect it's hidden by the magic of how the CLR
serializes/deserializes attributes. I've never tried putting any non-
trivial code in attribute properties.

The may be more in CLR via C#, which I'm reading at the moment... I'll
let you know if I find anything.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

Oct 8 '08 #5
Tony Johansson <jo*****************@telia.comwrote:
What I mean is from somewhere is the property and methods for the CodeStatus
called and
I don't know when the setter is called. It's possible it's called at
compile-time somehow, but the serialized form is then deserialized
without calling it again at execution time.
I can use reflection to ask for example the Rectangle class about all
attributes that is target that class.
Yes.
When I do so I can see that the custom attribute class has some data that
was set in some way by this
CodeStatus("Final", Coder="Bills")]
statement.
Indeed. It's possible that putting code in the getter will make it act
in a particular way - I don't know. I'd steer clear of that if I were
you though.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 8 '08 #6
Jon Skeet [C# MVP] wrote:
Tony Johansson <jo*****************@telia.comwrote:
>I just want to know when is the custom attibute CodeStatus called ?
It's only instantiated when you query the metadata.
What do you mean by "called"? It's a class - classes aren't "called",
methods are called.
>Do you mean that it's called in the background in some way that's way I
can't use the debugger.

Even if I can't use the debugger the CodeStatus attribute is loaded with
data from
CodeStatus("Final", Coder="Bills")]

Does it sound correct what I'm saying.

I suspect it's hidden by the magic of how the CLR
serializes/deserializes attributes. I've never tried putting any non-
trivial code in attribute properties.

The may be more in CLR via C#, which I'm reading at the moment... I'll
let you know if I find anything.
The IL essentially contains a reference to a constructor, the parameters
for that constructor, and a set of key-value pairs to populate the
properties (all of which are baked in). When you call
GetCustomAttributes(...) the CLR will create an instance of the
attribute and then call the setters for each property in turn (and so
every call to GetCustomAttributes will get a different instance).

The only real gotcha is the limitations on the types of the properties
and constructor parameters you can bake into an assembly (Type, string,
char, bool, byte, short, int, long, float, double and any enum based on
a CLS-compliant base integer type).

</IL geek>

Alun Harford
Oct 9 '08 #7

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

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
1
by: j erickson | last post by:
with the following xsl and xml file, the display of the gif file with the <image/url> tag works. However, the gif file in the <description> tag using the name attribute "src" won't make the correct...
4
by: Lénaïc Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello, I've some namespace problems when defining default values for attributes. My problem seems to come from the fact that the attributes are...
3
by: Tony Johansson | last post by:
Hello!! Assume we have one base class called Vehicle and two derived classes called Car and Bus. I would be able to call method getName on an object of class Car or Bus and return back the name...
1
by: arnold | last post by:
Hi, I've been knocking my head against the wall trying to create an XSL transform to perform "normalizations" of a set of XML files that have a common structure. % XML file before transform
5
by: Soledad Vel | last post by:
Hi All, i write this code: var sliderwidth=100; var sliderheight = 100; var div1 = document.createElement('div'); div1.setAttribute('id','d5'); div1.setAttribute('style',...
2
by: gary.goodwin | last post by:
HI I am trying to understand Attribute usage. For example the class SerializableAttribute is a class correct? So why when it is actually u sed the "Attribute" portion of the name is dropped. The...
0
by: Thomas Wittek | last post by:
Hi! I'm using xsl:attribute-sets to reduce redundancy in my XSLT. An example from a transformation to XHTML (the attribute values are simply copied from input to output): <xsl:attribute-set...
6
by: Adam Donahue | last post by:
As an exercise I'm attempting to write a metaclass that causes an exception to be thrown whenever a user tries to access 'attributes' (in the traditional sense) via a direct reference. Consider:...
18
by: Gabriel Rossetti | last post by:
Hello everyone, I had read somewhere that it is preferred to use self.__class__.attribute over ClassName.attribute to access class (aka static) attributes. I had done this and it seamed to work,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.