Attribute programming

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James Lapalme

    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


  • Jon Skeet

    #2
    Re: Attribute programming

    James Lapalme <james.lapalme@ videotron.ca> wrote:[color=blue]
    > 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....???[/color]

    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 - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Mikael

      #3
      Re: Attribute programming

      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.MessageBo x.Show("MyAttri bute Constructor");
      }
      public void Hello()
      {
      System.Windows. Forms.MessageBo x.Show("Hello from MyAttribute");
      }
      }

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

      // check if attribute exists
      if(type.IsDefin ed(typeof(MyAtt ribute), true))
      {
      // create an instance of the attribute
      MyAttribute tmp =
      type.GetCustomA ttributes(false )[0] as MyAttribute;

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


      /Mikael

      "James Lapalme" <james.lapalme@ videotron.ca> skrev i meddelandet
      news:%23WJiobCR DHA.3700@tk2msf tngp13.phx.gbl. ..[color=blue]
      > Hy,
      > I understand that attribute programming is used to add metadata to the[/color]
      code,[color=blue]
      > but since they are implemented with a class and we can extend that class,[/color]
      we[color=blue]
      > 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
      >
      >[/color]


      Comment

      Working...