1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
6 using System
.Reflection
;
10 partial class Attribute
12 static Attribute
? GetAttr (ICustomAttributeProvider element
, Type attributeType
, bool inherit
)
14 if (attributeType
== null)
15 throw new ArgumentNullException (nameof (attributeType
));
16 if (!attributeType
.IsSubclassOf (typeof (Attribute
)) && attributeType
!= typeof (Attribute
) && attributeType
!= typeof (CustomAttribute
))
17 throw new ArgumentException (SR
.Argument_MustHaveAttributeBaseClass
+ " " + attributeType
.FullName
);
19 var attrs
= CustomAttribute
.GetCustomAttributes (element
, attributeType
, inherit
);
20 if (attrs
== null || attrs
.Length
== 0)
22 if (attrs
.Length
!= 1)
23 throw new AmbiguousMatchException ();
24 return (Attribute
)(attrs
[0]);
27 public static Attribute
GetCustomAttribute (Assembly element
, Type attributeType
) => GetAttr (element
, attributeType
, true);
28 public static Attribute
GetCustomAttribute(Assembly element
, Type attributeType
, bool inherit
) => GetAttr (element
, attributeType
, inherit
);
29 public static Attribute
GetCustomAttribute(MemberInfo element
, Type attributeType
) => GetAttr (element
, attributeType
, true);
30 public static Attribute
GetCustomAttribute(MemberInfo element
, Type attributeType
, bool inherit
) => GetAttr (element
, attributeType
, inherit
);
31 public static Attribute
GetCustomAttribute(Module element
, Type attributeType
) => GetAttr (element
, attributeType
, true);
32 public static Attribute
GetCustomAttribute(Module element
, Type attributeType
, bool inherit
) => GetAttr (element
, attributeType
, inherit
);
33 public static Attribute
GetCustomAttribute(ParameterInfo element
, Type attributeType
) => GetAttr (element
, attributeType
, true);
34 public static Attribute
GetCustomAttribute(ParameterInfo element
, Type attributeType
, bool inherit
) => GetAttr (element
, attributeType
, inherit
);
36 public static Attribute
[] GetCustomAttributes (Assembly element
) => (Attribute
[])CustomAttribute
.GetCustomAttributes (element
, true);
37 public static Attribute
[] GetCustomAttributes (Assembly element
, bool inherit
) => (Attribute
[])CustomAttribute
.GetCustomAttributes (element
, inherit
);
38 public static Attribute
[] GetCustomAttributes (Assembly element
, Type attributeType
) => (Attribute
[])GetCustomAttributes ((ICustomAttributeProvider
)element
, attributeType
, true);
39 public static Attribute
[] GetCustomAttributes (Assembly element
, Type attributeType
, bool inherit
) => (Attribute
[])GetCustomAttributes ((ICustomAttributeProvider
)element
, attributeType
, inherit
);
40 public static Attribute
[] GetCustomAttributes (MemberInfo element
) => (Attribute
[])CustomAttribute
.GetCustomAttributes (element
, true);
41 public static Attribute
[] GetCustomAttributes (MemberInfo element
, bool inherit
) => (Attribute
[])CustomAttribute
.GetCustomAttributes (element
, inherit
);
42 public static Attribute
[] GetCustomAttributes (MemberInfo element
, Type attributeType
) => (Attribute
[])GetCustomAttributes ((ICustomAttributeProvider
)element
, attributeType
, true);
43 public static Attribute
[] GetCustomAttributes (MemberInfo element
, Type attributeType
, bool inherit
) => (Attribute
[])GetCustomAttributes ((ICustomAttributeProvider
)element
, attributeType
, inherit
);
44 public static Attribute
[] GetCustomAttributes (Module element
) => (Attribute
[])CustomAttribute
.GetCustomAttributes (element
, true);
45 public static Attribute
[] GetCustomAttributes (Module element
, bool inherit
) => (Attribute
[])CustomAttribute
.GetCustomAttributes (element
, inherit
);
46 public static Attribute
[] GetCustomAttributes (Module element
, Type attributeType
) => (Attribute
[])GetCustomAttributes ((ICustomAttributeProvider
)element
, attributeType
, true);
47 public static Attribute
[] GetCustomAttributes (Module element
, Type attributeType
, bool inherit
) => (Attribute
[])GetCustomAttributes ((ICustomAttributeProvider
)element
, attributeType
, inherit
);
48 public static Attribute
[] GetCustomAttributes (ParameterInfo element
) => (Attribute
[])CustomAttribute
.GetCustomAttributes (element
, true);
49 public static Attribute
[] GetCustomAttributes (ParameterInfo element
, bool inherit
) => (Attribute
[])CustomAttribute
.GetCustomAttributes (element
, inherit
);
50 public static Attribute
[] GetCustomAttributes (ParameterInfo element
, Type attributeType
) => (Attribute
[])GetCustomAttributes ((ICustomAttributeProvider
)element
, attributeType
, true);
51 public static Attribute
[] GetCustomAttributes (ParameterInfo element
, Type attributeType
, bool inherit
) => (Attribute
[])GetCustomAttributes ((ICustomAttributeProvider
)element
, attributeType
, inherit
);
53 internal static Attribute
[] GetCustomAttributes (ICustomAttributeProvider element
, Type attributeType
, bool inherit
)
55 if (attributeType
== null)
56 throw new ArgumentNullException (nameof (attributeType
));
57 if (!attributeType
.IsSubclassOf (typeof (Attribute
)) && attributeType
!= typeof (Attribute
))
58 throw new ArgumentException (SR
.Argument_MustHaveAttributeBaseClass
+ " " + attributeType
.FullName
);
60 return (Attribute
[])CustomAttribute
.GetCustomAttributes (element
, attributeType
, inherit
);
63 public static bool IsDefined (Assembly element
, Type attributeType
) => IsDefined ((ICustomAttributeProvider
)element
, attributeType
, true);
64 public static bool IsDefined (Assembly element
, Type attributeType
, bool inherit
) => IsDefined ((ICustomAttributeProvider
)element
, attributeType
, inherit
);
65 public static bool IsDefined (MemberInfo element
, Type attributeType
) => IsDefined ((ICustomAttributeProvider
)element
, attributeType
, true);
66 public static bool IsDefined (MemberInfo element
, Type attributeType
, bool inherit
) => IsDefined ((ICustomAttributeProvider
)element
, attributeType
, inherit
);
67 public static bool IsDefined (Module element
, Type attributeType
) => IsDefined ((ICustomAttributeProvider
)element
, attributeType
, true);
68 public static bool IsDefined (Module element
, Type attributeType
, bool inherit
) => IsDefined ((ICustomAttributeProvider
)element
, attributeType
, inherit
);
69 public static bool IsDefined (ParameterInfo element
, Type attributeType
) => IsDefined ((ICustomAttributeProvider
)element
, attributeType
, true);
70 public static bool IsDefined (ParameterInfo element
, Type attributeType
, bool inherit
) => IsDefined ((ICustomAttributeProvider
)element
, attributeType
, inherit
);
72 internal static bool IsDefined (ICustomAttributeProvider element
, Type attributeType
, bool inherit
)
74 if (attributeType
== null)
75 throw new ArgumentNullException (nameof (attributeType
));
76 if (!attributeType
.IsSubclassOf (typeof (Attribute
)) && attributeType
!= typeof (Attribute
))
77 throw new ArgumentException (SR
.Argument_MustHaveAttributeBaseClass
+ " " + attributeType
.FullName
);
79 return CustomAttribute
.IsDefined (element
, attributeType
, inherit
);