473,480 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 1669
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
3340
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
265
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
2408
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
9676
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
3383
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
2215
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
1636
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
11216
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
2154
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
7041
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
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
7043
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,...
0
7081
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...
1
6737
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
6921
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
5336
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,...
0
4481
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...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.