The Artima Developer Community
Sponsored Link

.NET Buzz Forum
L'ordine degli attributi multi-use in C#, VB, J#

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Adrian Florea

Posts: 206
Nickname: adrian11
Registered: Jul, 2004

Adrian Florea is a .NET developer from Italy
L'ordine degli attributi multi-use in C#, VB, J# Posted: Feb 13, 2006 8:25 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Adrian Florea.
Original Post: L'ordine degli attributi multi-use in C#, VB, J#
Feed Title: Web Log di Adrian Florea
Feed URL: /error.aspx?aspxerrorpath=/adrian/Rss.aspx
Feed Description: "You know you've achieved perfection in design, not when you have nothing more to add, but when you have nothing more to take away." Antoine de Saint-Exupery
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Adrian Florea
Latest Posts From Web Log di Adrian Florea

Advertisement

Ogni compilatore è libero di ordinare l'elenco di attributi multi-use che decorano un elemento di codice in base alle sue proprie regole. In questo senso, le specifiche di C# avvertono: "The order in which attributes are specified in an attribute section, and the order in which sections attached to the same program entity are arranged, is not significant" (ECMA-334, 24.2).

I seguenti 3 snippet equivalenti, scritti rispettivamente in C#, VB e J#:


using System;
 
[Foo(1), Foo(2), Foo(3), Foo(4), Foo(5), Foo(6), Foo(7), Foo(8), Foo(9), Foo(10)]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
class FooAttribute : Attribute
{
    public FooAttribute(int index)
    {
        _index = index;
    }
 
    private int _index;
    public int Index
    {
        get
        {
            return _index;
        }
    }
 
    static void Main()
    {
        Type fooAttributeType = typeof(FooAttribute);
        foreach (FooAttribute attribute in fooAttributeType.GetCustomAttributes(fooAttributeType, false))
        {
            Console.WriteLine(attribute.Index);
        }
    }
}


Imports System
 
<Foo(1), Foo(2), Foo(3), Foo(4), Foo(5), Foo(6), Foo(7), Foo(8), Foo(9), Foo(10)> _
<AttributeUsage(AttributeTargets.Class, AllowMultiple:=True)> _
Class FooAttribute
    Inherits Attribute
 
    Public Sub New(ByVal index As Integer)
        _index = index
    End Sub
 
    Private _index As Integer
    Public ReadOnly Property Index() As Integer
        Get
            Return _index
        End Get
    End Property
 
    Shared Sub Main()
        Dim fooAttributeType As Type = GetType(FooAttribute)
        For Each attribute As FooAttribute In fooAttributeType.GetCustomAttributes(fooAttributeType, False)
            Console.WriteLine(attribute.Index)
        Next
    End Sub
End Class


import System.*;
 
/** @attribute Foo(1) */
/** @attribute Foo(2) */
/** @attribute Foo(3) */
/** @attribute Foo(4) */
/** @attribute Foo(5) */
/** @attribute Foo(6) */
/** @attribute Foo(7) */
/** @attribute Foo(8) */
/** @attribute Foo(9) */
/** @attribute Foo(10) */
/** @attribute AttributeUsage(AttributeTargets.Class, AllowMultiple = true) */
class FooAttribute extends Attribute
{
      public FooAttribute(int index)
      {
            _index = index;
      }
 
      private int _index;
      /** @property */
      public int get_Index()
      {
            return _index;
      }
 
      public static void main()
      {
            Type fooAttributeType = Type.GetType("FooAttribute");
            Object[] attributes = fooAttributeType.GetCustomAttributes(fooAttributeType, false);
            for (int i = 0; i < attributes.get_Count(); i++)
            {
                  Console.WriteLine(((FooAttribute)attributes[i]).get_Index());
            }
      }
}


producono i seguenti risultati:

C# VB J#
1 9 1
4 10 2
5 8 3
6 7 4
7 6 5
8 5 6
9 4 7
10 3 8
2 2 9
3 1 10

Tra i tre compilatori, solo quello per J# sembra che non cambi l'ordine. Importante è non presupporre alcun ordine nell'elenco di attributi multi-use in una decorazione.

Read: L'ordine degli attributi multi-use in C#, VB, J#

Topic: Retention Server - Webcast Recording Previous Topic   Next Topic Topic: Mondosoft Acquires Taxonomy Software Provider Navigo Systems

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use