The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Decorando con enum

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
Decorando con enum Posted: Jul 5, 2005 1:18 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Adrian Florea.
Original Post: Decorando con enum
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

Come utilizzare un'enumeration come attribute?

Nello snippet seguente mostro un semplice ma elegante pattern per il wrapping di un'enumeration (ho inserito le spiegazioni come commenti nel codice).

Il fatto che il wrapper deriva da System.Attribute è solo per offrire una situazione concreta in cui il wrapping diventa utile e necessario. Con il valore 0 per il primo elemento dell'enumeration ho voluto ricordare una best practice spesso dimenticata: "ensure that 0 is a valid state for value types" (vedi l'item 8 del libro "Effective C#" di Bill Wagner).

using System;
 
public enum FooEnum
{
      Something = 0, // Default
      SomethingElse
}
 
public sealed class FooAttribute : Attribute
{
      // il campo enumeration su cui si fa il wrapping
      private readonly FooEnum _Foo;
      public FooEnum Foo {get{return _Foo;}}
 
      // private perché non è un elemento esplicito dell'enumeration
      private static readonly FooAttribute _Default = FooAttribute.Something;
 
      public static readonly FooAttribute Something = new FooAttribute(FooEnum.Something);
      public static readonly FooAttribute SomethingElse = new FooAttribute(FooEnum.SomethingElse);
     
      // l'enumeration ha un elemento di valore 0 (default)
      public FooAttribute() : this(new FooEnum()) { }
 
      public FooAttribute(FooEnum foo)
      {
            _Foo = foo;
      }
 
      public override bool IsDefaultAttribute()
      {
            return Equals(FooAttribute._Default);
      }
 
      public override bool Equals(object value)
      {
            if (value == this)
            {
                  return true;
            }
            if (!(value is FooAttribute))
            {
                  return false;
            }
            return (((FooAttribute)value).Foo == Foo);
      }
 
      // Equals e GetHashCode ridefiniti insieme, altrimenti warning CS0659
      public override int GetHashCode()
      {
            return base.GetHashCode();
      }
 
      // ToString dell'enumeration
      public override string ToString()
      {
            return Foo.ToString();
      }
}

Read: Decorando con enum

Topic: MVP Solutions Architect Previous Topic   Next Topic Topic: Microsoft Office Business Scorecard Server 2005 beta

Sponsored Links



Google
  Web Artima.com   

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