Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1562 | Anzahl registrierter User: 1893 | Besucher online: 33
Hauptmenü
Home
Top Ten
Zufälliger Snippet
FAQs
.NET Community
dotnet-forum.de
dotnet-kicks.de
Social

RSS Feeds
Rss Alle Snippets
Rss C#
Rss VB.NET
Rss C++
Rss ASP.NET
Partner
Member of Microsoft Community Leader/Insider Program (CLIP)

DescriptionAttribute von vorhandener Klasse übernehmen


Autor: Jürgen Thomas
Sprache: C#
Bewertung:
noch nicht bewertet
Anzahl der Aufrufe: 3763
  
Kick it on dotnet-kicks.de  

Beschreibung:

Mit diesem ReferencedDescription-Attribut wird für eine Eigenschaft anstelle des Description-Attributs der Text geholt, den NET für eine andere Klasse und Eigenschaft vorgemerkt hat. Beispiel: Für eine DataGridViewMaskedTextBoxColumn-Klasse wird die Beschreibung aus der MaskedTextBox.Mask-Eigenschaft angezeigt:

[ReferencedDescription(typeof(System.Windows.Forms.MaskedTextBox), "Mask")]


Abgelegt unter: DescriptionAttribute, Description, Attribute.



C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System.ComponentModel;

#region Referenced Description Attribute

public class ReferencedDescriptionAttribute : DescriptionAttribute
{

  public ReferencedDescriptionAttribute(Type referencedType, string propertyName)
  {
    string result = "Referenced description not available";

    //    gets the properties of the referenced type
    PropertyDescriptorCollection properties
            = TypeDescriptor.GetProperties(referencedType);

    if (properties != null) {
      // gets a PropertyDescriptor to the specific property.
      PropertyDescriptor property = properties[propertyName];
      if (property != null) {
        //  gets the attributes of the required property
        AttributeCollection attributes = property.Attributes;

        // Gets the description attribute from the collection.
        DescriptionAttribute descript
          = (DescriptionAttribute)attributes[typeof(DescriptionAttribute)];

        // register the referenced description
        if (!String.IsNullOrEmpty(descript.Description))
          result = descript.Description;
      }
    }
    DescriptionValue = result;

  }
}

#endregion
Sie haben Fragen zu diesem Snippet oder brauchen Hilfe bei der .NET Entwicklung?
Freundliche und kompetente Entwickler helfen Ihnen gern weiter im Forum für .NET Entwicklung.



Kommentare:
(Zum Schreiben von Kommentaren bitte anmelden.)



Diese Snippets könnten für Sie interessant sein:

schlecht sehr gut
1 2 3 4 5 6 7 8 9 10
Nur angemeldete User können Snippets bewerten.