2015-02-24 5 views
9

Ich habe eine SQL-Lookup-Tabelle namens ClientCreditResolutionPlanActionType, die ich in in konvertieren möchte.Wie füge ich einem Enum mehrere Attribute hinzu?

Sehr einfache Anfrage, oder? Recht.

Mein Tisch, jetzt jedoch mehrere Spalten hat, oder jetzt, Beschreibung Eigenschaften, die mit ihm gehen müssen:

  • StatusIcon
  • Statustext
  • Type

Also ich dachte, ich könnte tun ...

namespace System.ComponentModel 
{ 
    class StatusIconAttribute : Attribute 
    { 
     public string StatusIcon; 
     public StatusIconAttribute(string statusIcon) { StatusIcon = statusIcon; } 
    } 

    class StatusTextAttribute : Attribute 
    { 
     public string StatusText; 
     public StatusTextAttribute(string statusText) { StatusText = statusText; } 
    } 

    class TypeTextAttribute : Attribute 
    { 
     public string TypeText; 
     public TypeTextAttribute(string typeText) { TypeText = typeText; } 
    } 
} 

... in meiner Extensions.cs Klasse ...

public static class EnumExtensions 
{ 
    public static string GetStatusIcon(this Enum value) 
    { 
     var type = value.GetType(); 

     string name = Enum.GetName(type, value);    
     if (name == null) { return null; } 

     var field = type.GetField(name); 
     if (field == null) { return null; } 

     var attr = Attribute.GetCustomAttribute(field, typeof(StatusIconAttribute)) as StatusIconAttribute; 
     if (attr == null) { return null; } 

     return attr.StatusIcon; 
    } 

    public static string GetStatusText(this Enum value) 
    { 
     var type = value.GetType(); 

     string name = Enum.GetName(type, value);    
     if (name == null) { return null; } 

     var field = type.GetField(name); 
     if (field == null) { return null; } 

     var attr = Attribute.GetCustomAttribute(field, typeof(StatusTextAttribute)) as StatusTextAttribute; 
     if (attr == null) { return null; } 

     return attr.StatusText; 
    } 

    public static string GetTypeText(this Enum value) 
    { 
     var type = value.GetType(); 
     string name = Enum.GetName(type, value);    

     var type = value.GetType(); 

     string name = Enum.GetName(type, value);    
     if (name == null) { return null; } 

     var field = type.GetField(name); 
     if (field == null) { return null; } 

     var attr = Attribute.GetCustomAttribute(field, typeof(TypeTextAttribute)) as TypeTextAttribute; 
     if (attr == null) { return null; } 

     return attr.TypeText; 
    } 
} 

... und schließlich in meinem anderen Projekt es gerne verwenden:

namespace ClientSystemServiceLibrary.Enums 
{ 
    [DataContract] 
    public enum ClientCreditResolutionPlanActionType 
    { 
     [EnumMember] 
     [TypeText("New resolution plan submitted.")] 
     [StatusText("New Plan")] 
     [StatusIcon("star.png")] 
     NewPlan = 1, 

     [EnumMember] 
     [TypeText("Resolution plan waiting on approval.")] 
     [StatusText("Under Review")] 
     [StatusIcon("review.png")] 
     UnderReview = 2, 

     [EnumMember] 
     [TypeText("Resolution plan approved.")] 
     [StatusText("Approved")] 
     [StatusIcon("check.png")] 
     Approved = 3, 

     [EnumMember] 
     [TypeText("Resolution plan rejected.")] 
     [StatusText("Rejected")] 
     [StatusIcon("cross.png")] 
     Rejected = 4, 

     [EnumMember] 
     [TypeText("New resolution plan comment submitted.")] 
     [StatusText("New Comment")] 
     [StatusIcon("message.png")] 
     NewComment = 5 
    } 
}E 

Außer, was ich figürlich war falsch, da ich folgende Fehlermeldungen erhalte:

'System.CompenentModel.TypeT extAttribute‘unzugänglich ist aufgrund seiner Schutzstufe

und

Der Typ oder Namespace-Name‚Typetext‘konnte nicht gefunden werden (möglicherweise fehlt eine using-Direktive oder ein Assemblyverweis?)

Gleich ... für alle 3.

+1

Was meinst du, "was ich dachte, war falsch"? – Blorgbeard

+0

@Blorgbeard - Erläuterung über Fehlermeldungen hinzugefügt; Das tut mir leid! –

+1

Warum überhaupt zu einem Enum konvertieren? Warum nicht vom Tisch lesen? – Brad

Antwort

7

Standardmäßig sind alle Klassen intern. Sie sollten den Zugriffsmodifikator "public" angeben, wenn Sie auf andere Assemblys zugreifen möchten. Gefällt mir:

public class TypeTextAttribute : Attribute 
{ 
    public string TypeText; 
    public TypeTextAttribute(string typeText) { TypeText = typeText; } 
} 
+0

genau das, was ich gerade getan habe ... Ich kann deine nicht als die Antwort markieren, weil ich es vorher funktioniert habe, aber ich werde definitiv +1 –

+1

oh was zum Teufel ... Ich werde meine löschen und gebe Ihnen den Scheck = D –

+0

Jetzt ... ist die einzige Einschränkung, dass Sie die Anweisung using directive oder assembly hinzufügen müssen, von der dieser Code in der Codedatei stammt, in der Sie Ihre enum erstellen. –