PC Review


Reply
Thread Tools Rate Thread

Attribute programming

 
 
James Lapalme
Guest
Posts: n/a
 
      7th Jul 2003
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


 
Reply With Quote
 
 
 
 
Jon Skeet
Guest
Posts: n/a
 
      7th Jul 2003
James Lapalme <(E-Mail Removed)> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
 
Reply With Quote
 
Mikael
Guest
Posts: n/a
 
      7th Jul 2003
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" <(E-Mail Removed)> skrev i meddelandet
news:%(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Validation (XHTML 1.0 Transitional): Attribute 'height' is not a valid attribute of element 'table' perspolis Microsoft ASP .NET 1 23rd Jan 2007 07:57 AM
Validation (XHTML 1.0 Transitional): Attribute 'leftmargin' is not a valid attribute of element 'body'. anonymous Microsoft ASP .NET 1 2nd Aug 2006 09:05 AM
Attribute Based Programming - Tracing =?Utf-8?B?S2Vu?= Microsoft Dot NET 3 23rd Mar 2006 01:11 AM
Attribute programming James Lapalme Microsoft Dot NET 2 7th Jul 2003 08:28 AM
Attribute programming James Lapalme Microsoft C# .NET 2 7th Jul 2003 08:28 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:37 PM.