Windows Azure Cloud Storage ermöglicht es Ihnen bereits ab 0,10€ pro GB/Monat die Vorteile der Cloud zu nutzen.
Willkommen bei dotnet-snippets.de! Snippet hinzufügen Login Registrieren
Snippets in der Datenbank: 1550 | Anzahl registrierter User: 1841 | Besucher online: 70
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)

Attribute zur Laufzeit abfragen


Autor: Thorsten Hans
Sprache: C#
Bewertung: 5,8
(1 Bewertung)
Anzahl der Aufrufe: 7497
  
Kick it on dotnet-kicks.de  

Beschreibung:

Mit Hilfe dieser Klasse könnt Ihr zur Laufzeit prüfen ob Attribute auf Klassen definiert sind, falls dies der Fall ist könnt ihr mit den anderen beiden Methoden abfragen ob das Value des Attributes true ist oder das Value als string auslesen.

Abgelegt unter: Attributes, Attribute, Laufzeit.



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
38
39
40
41
42
43
44
45
46
47
  internal class AttributeLogic
    {
        internal static readonly AttributeLogic Instance = new AttributeLogic();

        internal string GetAttributeValue(Type targetType, Type attributeType, string attributePropertyName)
        {
            if (Attribute.IsDefined(targetType, attributeType))
            {
                Object att = Attribute.GetCustomAttribute(targetType, attributeType);
                PropertyInfo pi = att.GetType().GetProperty(attributePropertyName);
                if (pi == null)
                {
                    throw new Exception(String.Format("The {0} attribute on the {1} type dosen't define the {2} property",
                        new object[] { attributeType, targetType, attributePropertyName }));
                }
                else
                {
                    return pi.GetValue(att, null).ToString();
                }
            }
            else
            {
                throw new Exception(String.Format("The {0} attribute isn't set to the type {1}", new object[] { attributeType, targetType }));
            }
        }

        internal bool isAttributeTrue(Type targetType, Type attributeType, string attributePropertyName)
        {
            if (Attribute.IsDefined(targetType, attributeType))
            {
                Object att = Attribute.GetCustomAttribute(targetType, attributeType);
                PropertyInfo pi = att.GetType().GetProperty(attributePropertyName);
                if (pi == null)
                {
                    return false;
                }
                else
                {
                    return Convert.ToBoolean(pi.GetValue(att, null));
                }
            }
            else
            {
                return false;
            }
        }
    }
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.)

yoka schrieb am:  17.05.2008 11:29:16

Zur Laufzeit ist das ja ganz nett -no offense- aber zur Compile Zeit Attributes abzufragen ist viel spannender! Damit könnte man den eigenen Code endlich in Stufe 2 Grammatiken umwandeln, oder zumindest pre/post conditions definieren! Das wär n echter Hammer!
Rainer Schuster schrieb am:  21.05.2008 08:50:59

@Yoka: Dann schau dir mal PostSharp an.


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