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.
4 
5 //
6 // The older-style CustomAttribute-related members on the various Reflection types. The implementation dependency
7 // stack on .Net Native differs from that of CoreClr due to the difference in development history.
8 //
9 // - IEnumerable<CustomAttributeData> xInfo.get_CustomAttributes is at the very bottom of the dependency stack.
10 //
11 // - CustomAttributeExtensions layers on top of that (primarily because it's the one with the nice generic methods.)
12 //
13 // - Everything else is a thin layer over one of these two.
14 //
15 //
16 
17 using System;
18 using System.IO;
19 using System.Reflection;
20 using System.Collections.Generic;
21 using System.Reflection.Runtime.General;
22 
23 using Internal.LowLevelLinq;
24 using Internal.Reflection.Extensions.NonPortable;
25 
26 
27 namespace System.Reflection.Runtime.Assemblies
28 {
29     internal partial class RuntimeAssembly
30     {
GetCustomAttributesData()31         public sealed override IList<CustomAttributeData> GetCustomAttributesData() => CustomAttributes.ToReadOnlyCollection();
32         public sealed override object[] GetCustomAttributes(bool inherit) => CustomAttributeExtensions.GetCustomAttributes(this).ToArray();  // inherit is meaningless for Assemblies
33 
GetCustomAttributes(Type attributeType, bool inherit)34         public sealed override object[] GetCustomAttributes(Type attributeType, bool inherit)
35         {
36             if (attributeType == null)
37                 throw new ArgumentNullException(nameof(attributeType));
38             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, skipTypeValidation: true); // inherit is meaningless for Assemblies
39             return cads.InstantiateAsArray(attributeType);
40         }
41 
IsDefined(Type attributeType, bool inherit)42         public sealed override bool IsDefined(Type attributeType, bool inherit)
43         {
44             if (attributeType == null)
45                 throw new ArgumentNullException(nameof(attributeType));
46             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, skipTypeValidation: true); // inherit is meaningless for Assemblies
47             return cads.Any();
48         }
49     }
50 }
51 
52 namespace System.Reflection.Runtime.MethodInfos
53 {
54     internal abstract partial class RuntimeConstructorInfo
55     {
GetCustomAttributesData()56         public sealed override IList<CustomAttributeData> GetCustomAttributesData() => CustomAttributes.ToReadOnlyCollection();
57         public sealed override object[] GetCustomAttributes(bool inherit) => CustomAttributeExtensions.GetCustomAttributes(this, inherit).ToArray();
58 
GetCustomAttributes(Type attributeType, bool inherit)59         public sealed override object[] GetCustomAttributes(Type attributeType, bool inherit)
60         {
61             if (attributeType == null)
62                 throw new ArgumentNullException(nameof(attributeType));
63             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, inherit: inherit, skipTypeValidation: true);
64             return cads.InstantiateAsArray(attributeType);
65         }
66 
IsDefined(Type attributeType, bool inherit)67         public sealed override bool IsDefined(Type attributeType, bool inherit)
68         {
69             if (attributeType == null)
70                 throw new ArgumentNullException(nameof(attributeType));
71             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, inherit: inherit, skipTypeValidation: true);
72             return cads.Any();
73         }
74     }
75 }
76 
77 namespace System.Reflection.Runtime.EventInfos
78 {
79     internal abstract partial class RuntimeEventInfo
80     {
GetCustomAttributesData()81         public sealed override IList<CustomAttributeData> GetCustomAttributesData() => CustomAttributes.ToReadOnlyCollection();
82         public sealed override object[] GetCustomAttributes(bool inherit) => CustomAttributeExtensions.GetCustomAttributes(this, inherit: false).ToArray();  // Desktop compat: for events, this form of the api ignores "inherit"
83 
GetCustomAttributes(Type attributeType, bool inherit)84         public sealed override object[] GetCustomAttributes(Type attributeType, bool inherit)
85         {
86             if (attributeType == null)
87                 throw new ArgumentNullException(nameof(attributeType));
88             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, inherit: false, skipTypeValidation: true); // Desktop compat: for events, this form of the api ignores "inherit"
89             return cads.InstantiateAsArray(attributeType);
90         }
91 
IsDefined(Type attributeType, bool inherit)92         public sealed override bool IsDefined(Type attributeType, bool inherit)
93         {
94             if (attributeType == null)
95                 throw new ArgumentNullException(nameof(attributeType));
96             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, inherit: false, skipTypeValidation: true); // Desktop compat: for events, this form of the api ignores "inherit"
97             return cads.Any();
98         }
99     }
100 }
101 
102 namespace System.Reflection.Runtime.FieldInfos
103 {
104     internal abstract partial class RuntimeFieldInfo
105     {
GetCustomAttributesData()106         public sealed override IList<CustomAttributeData> GetCustomAttributesData() => CustomAttributes.ToReadOnlyCollection();
107         public sealed override object[] GetCustomAttributes(bool inherit) => CustomAttributeExtensions.GetCustomAttributes(this, inherit).ToArray();
108 
GetCustomAttributes(Type attributeType, bool inherit)109         public sealed override object[] GetCustomAttributes(Type attributeType, bool inherit)
110         {
111             if (attributeType == null)
112                 throw new ArgumentNullException(nameof(attributeType));
113             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, inherit: inherit, skipTypeValidation: true);
114             return cads.InstantiateAsArray(attributeType);
115         }
116 
IsDefined(Type attributeType, bool inherit)117         public sealed override bool IsDefined(Type attributeType, bool inherit)
118         {
119             if (attributeType == null)
120                 throw new ArgumentNullException(nameof(attributeType));
121             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, inherit: inherit, skipTypeValidation: true);
122             return cads.Any();
123         }
124     }
125 }
126 
127 namespace System.Reflection.Runtime.MethodInfos
128 {
129     internal abstract partial class RuntimeMethodInfo
130     {
GetCustomAttributesData()131         public sealed override IList<CustomAttributeData> GetCustomAttributesData() => CustomAttributes.ToReadOnlyCollection();
132         public sealed override object[] GetCustomAttributes(bool inherit) => CustomAttributeExtensions.GetCustomAttributes(this, inherit).ToArray();
133 
GetCustomAttributes(Type attributeType, bool inherit)134         public sealed override object[] GetCustomAttributes(Type attributeType, bool inherit)
135         {
136             if (attributeType == null)
137                 throw new ArgumentNullException(nameof(attributeType));
138             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, inherit: inherit, skipTypeValidation: true);
139             return cads.InstantiateAsArray(attributeType);
140         }
141 
IsDefined(Type attributeType, bool inherit)142         public sealed override bool IsDefined(Type attributeType, bool inherit)
143         {
144             if (attributeType == null)
145                 throw new ArgumentNullException(nameof(attributeType));
146             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, inherit: inherit, skipTypeValidation: true);
147             return cads.Any();
148         }
149     }
150 }
151 
152 namespace System.Reflection.Runtime.Modules
153 {
154     internal abstract partial class RuntimeModule
155     {
GetCustomAttributesData()156         public sealed override IList<CustomAttributeData> GetCustomAttributesData() => CustomAttributes.ToReadOnlyCollection();
157         public sealed override object[] GetCustomAttributes(bool inherit) => CustomAttributeExtensions.GetCustomAttributes(this).ToArray();  // inherit is meaningless for Modules
158 
GetCustomAttributes(Type attributeType, bool inherit)159         public sealed override object[] GetCustomAttributes(Type attributeType, bool inherit)
160         {
161             if (attributeType == null)
162                 throw new ArgumentNullException(nameof(attributeType));
163             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, skipTypeValidation: true); // inherit is meaningless for Modules
164             return cads.InstantiateAsArray(attributeType);
165         }
166 
IsDefined(Type attributeType, bool inherit)167         public sealed override bool IsDefined(Type attributeType, bool inherit)
168         {
169             if (attributeType == null)
170                 throw new ArgumentNullException(nameof(attributeType));
171             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, skipTypeValidation: true); // inherit is meaningless for Modules
172             return cads.Any();
173         }
174     }
175 }
176 
177 namespace System.Reflection.Runtime.ParameterInfos
178 {
179     internal abstract partial class RuntimeParameterInfo
180     {
GetCustomAttributesData()181         public sealed override IList<CustomAttributeData> GetCustomAttributesData() => CustomAttributes.ToReadOnlyCollection();
182         public sealed override object[] GetCustomAttributes(bool inherit) => CustomAttributeExtensions.GetCustomAttributes(this, inherit: false).ToArray(); // Desktop compat: for parameters, this form of the api ignores "inherit"
183 
GetCustomAttributes(Type attributeType, bool inherit)184         public sealed override object[] GetCustomAttributes(Type attributeType, bool inherit)
185         {
186             if (attributeType == null)
187                 throw new ArgumentNullException(nameof(attributeType));
188             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, inherit: false, skipTypeValidation: true); // Desktop compat: for parameters, this form of the api ignores "inherit"
189             return cads.InstantiateAsArray(attributeType);
190         }
191 
IsDefined(Type attributeType, bool inherit)192         public sealed override bool IsDefined(Type attributeType, bool inherit)
193         {
194             if (attributeType == null)
195                 throw new ArgumentNullException(nameof(attributeType));
196             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, inherit: false, skipTypeValidation: true); // Desktop compat: for parameters, this form of the api ignores "inherit"
197             return cads.Any();
198         }
199     }
200 }
201 
202 namespace System.Reflection.Runtime.PropertyInfos
203 {
204     internal abstract partial class RuntimePropertyInfo
205     {
GetCustomAttributesData()206         public sealed override IList<CustomAttributeData> GetCustomAttributesData() => CustomAttributes.ToReadOnlyCollection();
207         public sealed override object[] GetCustomAttributes(bool inherit) => CustomAttributeExtensions.GetCustomAttributes(this, inherit: false).ToArray(); // Desktop compat: for properties, this form of the api ignores "inherit"
208 
GetCustomAttributes(Type attributeType, bool inherit)209         public sealed override object[] GetCustomAttributes(Type attributeType, bool inherit)
210         {
211             if (attributeType == null)
212                 throw new ArgumentNullException(nameof(attributeType));
213             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, inherit: false, skipTypeValidation: true); // Desktop compat: for properties, this form of the api ignores "inherit"
214             return cads.InstantiateAsArray(attributeType);
215         }
216 
IsDefined(Type attributeType, bool inherit)217         public sealed override bool IsDefined(Type attributeType, bool inherit)
218         {
219             if (attributeType == null)
220                 throw new ArgumentNullException(nameof(attributeType));
221             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, inherit: false, skipTypeValidation: true); // Desktop compat: for properties, this form of the api ignores "inherit"
222             return cads.Any();
223         }
224     }
225 }
226 
227 namespace System.Reflection.Runtime.TypeInfos
228 {
229     internal abstract partial class RuntimeTypeInfo
230     {
GetCustomAttributesData()231         public sealed override IList<CustomAttributeData> GetCustomAttributesData() => CustomAttributes.ToReadOnlyCollection();
232         public sealed override object[] GetCustomAttributes(bool inherit) => CustomAttributeExtensions.GetCustomAttributes(this, inherit).ToArray();
233 
GetCustomAttributes(Type attributeType, bool inherit)234         public sealed override object[] GetCustomAttributes(Type attributeType, bool inherit)
235         {
236             if (attributeType == null)
237                 throw new ArgumentNullException(nameof(attributeType));
238             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, inherit: inherit, skipTypeValidation: true);
239             return cads.InstantiateAsArray(attributeType);
240         }
241 
IsDefined(Type attributeType, bool inherit)242         public sealed override bool IsDefined(Type attributeType, bool inherit)
243         {
244             if (attributeType == null)
245                 throw new ArgumentNullException(nameof(attributeType));
246             IEnumerable<CustomAttributeData> cads = this.GetMatchingCustomAttributes(attributeType, inherit: inherit, skipTypeValidation: true);
247             return cads.Any();
248         }
249     }
250 }
251