473,406 Members | 2,619 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,406 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
Jul 19 '05 #1
2 1666
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
Jul 19 '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

Jul 19 '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...
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: 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?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.