By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,668 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,668 IT Pros & Developers. It's quick & easy.

Attribute programming

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.