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

Attribute programming

Hy,
I understand that attribute programming is used to add metadata to the code,
but since they are implemented with a class and we can extend that class, we
should be able to do any normal class thing with attributes like:

-Have methods
-Use any kind of code in the constructor and the methods (ex. Use a
MessageBox)

But the run time will not execute a MessageBox in the constructor?? why?
Where is it defiened that and attribute can only have fields (properties)
and that the constructor is used only to init these
fields....???

James
Nov 13 '05 #1
2 5826
James Lapalme <ja***********@videotron.ca> wrote:
I understand that attribute programming is used to add metadata to the code,
but since they are implemented with a class and we can extend that class, we
should be able to do any normal class thing with attributes like:

-Have methods
-Use any kind of code in the constructor and the methods (ex. Use a
MessageBox)

But the run time will not execute a MessageBox in the constructor?? why?
Where is it defiened that and attribute can only have fields (properties)
and that the constructor is used only to init these
fields....???


An attribute can have any kind of constructor you like, and can have
any methods you like. However, when you *specify* an attribute for a
field/type/method etc, you're limited as to which types can be passed
in the constructor.

See section 22.3 of partition 2 of the ECMA CLI spec (ECMA 335) for
more details.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #2
Hi James,

Attributes can have methods and use any kind of code in the constructor.

This compiles/runs just fine on my machine.

using System;

public class MyAttribute : Attribute
{
public MyAttribute()
{
System.Windows.Forms.MessageBox.Show("MyAttribute Constructor");
}
public void Hello()
{
System.Windows.Forms.MessageBox.Show("Hello from MyAttribute");
}
}

[MyAttribute()]
class AttributeTest
{
static void Main()
{
Type type = typeof(AttributeTest);

// check if attribute exists
if(type.IsDefined(typeof(MyAttribute), true))
{
// create an instance of the attribute
MyAttribute tmp =
type.GetCustomAttributes(false)[0] as MyAttribute;

// call method on attribute
tmp.Hello();
}
}
}
/Mikael

"James Lapalme" <ja***********@videotron.ca> skrev i meddelandet
news:%2****************@tk2msftngp13.phx.gbl...
Hy,
I understand that attribute programming is used to add metadata to the code, but since they are implemented with a class and we can extend that class, we should be able to do any normal class thing with attributes like:

-Have methods
-Use any kind of code in the constructor and the methods (ex. Use a
MessageBox)

But the run time will not execute a MessageBox in the constructor?? why?
Where is it defiened that and attribute can only have fields (properties)
and that the constructor is used only to init these
fields....???

James

Nov 13 '05 #3

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

Similar topics

6
by: Coral Snake | last post by:
I am having problems with programming even simple "Hello World" programs from books and tutorials that use Python GUI libraries. Such Programs cause python to throw "Attribute Errors" even when the...
4
by: Donnal Walter | last post by:
This is a question about Python patterns or idioms. Over a period of time, I have evolved a pattern of usage that seems to work better for me than other ways I tried previously, but in writing some...
2
by: James Lapalme | last post by:
Hy, I understand that attribute programming is used to add metadata to the code, but since they are implemented with a class and we can extend that class, we should be able to do any normal class...
2
by: Wouter de Kort | last post by:
I've started looking at Aspect Oriented Programming but I've stumbled at a problem I can't solve. I want to add an Aspect that would notify a UnitOfWork that the current DomainObject has changed...
2
by: belgie | last post by:
Whereas in Asp I could submit my form contents to any other Asp page, it appears that in Asp.NET the primary Asp form will only submit to itself. I have tried changing the Action attribute in the...
2
by: Neil Zanella | last post by:
Hello, The book "Programming ASP.NET" by O'Reilly, an excellent source of ASP.NET information indeed, specifies that along with the runat="server" attribute an id attribute should also be...
3
by: chat_devil | last post by:
hi, does anyone know if it is possible to remove an attribute that can not be read into the ADSI property cache/collection. i'm trying to do an eDirectory password change from .net directory...
4
by: Theo | last post by:
Hi, i created a custom attribute that accepts an argument in the constructor to fill a property. The constructor is supposed to be called when then attribute is set but that doesn't seem to happen....
4
by: Walton | last post by:
checkout: (3rd paragraph under 'Remarks' section) http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/properties/name_2.asp from the page: "The NAME attribute...
5
by: elbin | last post by:
Hello, first to say that I am a total beginner in Javascript but I know some programming (python in particular) and am able to understand methods/parameters and so on. Here's my problem: I am...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.