1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.Metadata
7 using System.Collections.Generic;
9 using System.Diagnostics;
12 // This class groups Attribute with its AttributeUsageAttributes that we care about.
13 // Basically, each Attribute has characteristics that are useful to know about,
14 // such as whether it can be inherited and whether there can be more than
15 // one instance of that attribute extending whatever it is the attribute
16 // is extending (class, method, property, or event). Those characteristics
17 // are stored as attributes themselves and, as such, are costly to retrieve.
18 // This class retrieves that information exactly once, on demand, and caches it for
20 internal class AttributeData
23 private Type _attributeType;
24 private bool? _isInheritable;
25 private bool? _allowsMultiple;
28 // Creates an AttributeData wrapper around Attribute type to expose its
29 // Inherit and AllowMultiple characteristics
31 // <param name="attributeType">Attribute type to wrap around</param>
32 internal AttributeData(Type attributeType)
34 Fx.Assert(attributeType != null, "attributeType parameter should not be null");
35 _attributeType = attributeType;
39 // Gets the contained attribute type
41 internal Type AttributeType
44 return _attributeType;
49 // Gets the AllowMultiple characteristic of the
50 // contained attribute and caches the result for subsequent
51 // calls to this property.
53 internal bool AllowsMultiple
56 if (_allowsMultiple == null)
58 ParseUsageAttributes();
61 return (bool)_allowsMultiple;
66 // Gets the Inherit characteristic of the
67 // contained attribute and caches the result for subsequent
68 // calls to this property.
70 internal bool IsInheritable
73 if (_isInheritable == null)
75 ParseUsageAttributes();
78 return (bool)_isInheritable;
82 private void ParseUsageAttributes()
84 _isInheritable = false;
85 _allowsMultiple = false;
86 object[] usageAttributes = _attributeType.GetCustomAttributes(typeof(AttributeUsageAttribute), true);
88 if (usageAttributes != null && usageAttributes.Length > 0)
90 for (int i = 0; i < usageAttributes.Length; i++)
92 Fx.Assert(usageAttributes[i] is AttributeUsageAttribute, "usageAttributes should be of type AttributeUsageAttribute");
93 AttributeUsageAttribute usageAttribute = (AttributeUsageAttribute)usageAttributes[i];
94 _isInheritable = usageAttribute.Inherited;
95 _allowsMultiple = usageAttribute.AllowMultiple;