Skip to content

Commit 0844cb7

Browse files
authored
[reflection] Convert internal GetCustomAttributes calls to Attribute[] (mono/mono#18176)
* [reflection] Avoid creating object[] in GetCustomAttributesBase * [reflection] Migrate Attribute type checking to CustomAttribute.cs Commit migrated from mono/mono@ff6294d
1 parent 4f79325 commit 0844cb7

File tree

3 files changed

+27
-51
lines changed

3 files changed

+27
-51
lines changed

src/mono/netcore/CoreFX.issues.rsp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -584,15 +584,6 @@
584584
# same issue (only System.ComponentModel.Composition uses this namespace)
585585
-nonamespace Tests.Integration
586586

587-
####################################################################
588-
## System.Composition.TypedParts.Tests
589-
####################################################################
590-
591-
# Missing assembly Microsoft.VisualStudio.TestPlatform.ObjectModel, Version=15.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
592-
# https://github.com/mono/mono/issues/15170
593-
-nomethod System.Composition.Hosting.Tests.ContainerConfigurationTests.WithAssemblies_Assemblies_ThrowsCompositionFailedExceptionOnCreation
594-
-nomethod System.Composition.Hosting.Tests.ContainerConfigurationTests.WithAssembly_Assembly_ThrowsCompositionFailedExceptionOnCreation
595-
596587
####################################################################
597588
## System.Data.Common.Tests
598589
####################################################################

src/mono/netcore/System.Private.CoreLib/src/System/Attribute.Mono.cs

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,48 +35,28 @@ partial class Attribute
3535

3636
public static Attribute[] GetCustomAttributes (Assembly element) => (Attribute[])CustomAttribute.GetCustomAttributes (element, true);
3737
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);
38+
public static Attribute[] GetCustomAttributes (Assembly element, Type attributeType) => (Attribute[])CustomAttribute.GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, true);
39+
public static Attribute[] GetCustomAttributes (Assembly element, Type attributeType, bool inherit) => (Attribute[])CustomAttribute.GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, inherit);
4040
public static Attribute[] GetCustomAttributes (MemberInfo element) => (Attribute[])CustomAttribute.GetCustomAttributes (element, true);
4141
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);
42+
public static Attribute[] GetCustomAttributes (MemberInfo element, Type attributeType) => (Attribute[])CustomAttribute.GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, true);
43+
public static Attribute[] GetCustomAttributes (MemberInfo element, Type attributeType, bool inherit) => (Attribute[])CustomAttribute.GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, inherit);
4444
public static Attribute[] GetCustomAttributes (Module element) => (Attribute[])CustomAttribute.GetCustomAttributes (element, true);
4545
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);
46+
public static Attribute[] GetCustomAttributes (Module element, Type attributeType) => (Attribute[])CustomAttribute.GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, true);
47+
public static Attribute[] GetCustomAttributes (Module element, Type attributeType, bool inherit) => (Attribute[])CustomAttribute.GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, inherit);
4848
public static Attribute[] GetCustomAttributes (ParameterInfo element) => (Attribute[])CustomAttribute.GetCustomAttributes (element, true);
4949
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);
52-
53-
internal static Attribute[] GetCustomAttributes (ICustomAttributeProvider element, Type attributeType, bool inherit)
54-
{
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);
59-
60-
return (Attribute[])CustomAttribute.GetCustomAttributes (element, attributeType, inherit);
61-
}
62-
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);
71-
72-
internal static bool IsDefined (ICustomAttributeProvider element, Type attributeType, bool inherit)
73-
{
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);
78-
79-
return CustomAttribute.IsDefined (element, attributeType, inherit);
80-
}
50+
public static Attribute[] GetCustomAttributes (ParameterInfo element, Type attributeType) => (Attribute[])CustomAttribute.GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, true);
51+
public static Attribute[] GetCustomAttributes (ParameterInfo element, Type attributeType, bool inherit) => (Attribute[])CustomAttribute.GetCustomAttributes ((ICustomAttributeProvider)element, attributeType, inherit);
52+
53+
public static bool IsDefined (Assembly element, Type attributeType) => CustomAttribute.IsDefined ((ICustomAttributeProvider)element, attributeType, true);
54+
public static bool IsDefined (Assembly element, Type attributeType, bool inherit) => CustomAttribute.IsDefined ((ICustomAttributeProvider)element, attributeType, inherit);
55+
public static bool IsDefined (MemberInfo element, Type attributeType) => CustomAttribute.IsDefined ((ICustomAttributeProvider)element, attributeType, true);
56+
public static bool IsDefined (MemberInfo element, Type attributeType, bool inherit) => CustomAttribute.IsDefined ((ICustomAttributeProvider)element, attributeType, inherit);
57+
public static bool IsDefined (Module element, Type attributeType) => CustomAttribute.IsDefined ((ICustomAttributeProvider)element, attributeType, true);
58+
public static bool IsDefined (Module element, Type attributeType, bool inherit) => CustomAttribute.IsDefined ((ICustomAttributeProvider)element, attributeType, inherit);
59+
public static bool IsDefined (ParameterInfo element, Type attributeType) => CustomAttribute.IsDefined ((ICustomAttributeProvider)element, attributeType, true);
60+
public static bool IsDefined (ParameterInfo element, Type attributeType, bool inherit) => CustomAttribute.IsDefined ((ICustomAttributeProvider)element, attributeType, inherit);
8161
}
8262
}

src/mono/netcore/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ static bool IsUserCattrProvider (object obj)
5353
}
5454

5555
[MethodImplAttribute (MethodImplOptions.InternalCall)]
56-
internal static extern object[] GetCustomAttributesInternal (ICustomAttributeProvider obj, Type attributeType, bool pseudoAttrs);
56+
internal static extern Attribute[] GetCustomAttributesInternal (ICustomAttributeProvider obj, Type attributeType, bool pseudoAttrs);
5757

5858
internal static object[] GetPseudoCustomAttributes (ICustomAttributeProvider obj, Type attributeType) {
5959
object[] pseudoAttrs = null;
@@ -120,7 +120,7 @@ internal static object[] GetCustomAttributesBase (ICustomAttributeProvider obj,
120120
if (!inheritedOnly) {
121121
object[] pseudoAttrs = GetPseudoCustomAttributes (obj, attributeType);
122122
if (pseudoAttrs != null) {
123-
object[] res = new object [attrs.Length + pseudoAttrs.Length];
123+
object[] res = new Attribute [attrs.Length + pseudoAttrs.Length];
124124
System.Array.Copy (attrs, res, attrs.Length);
125125
System.Array.Copy (pseudoAttrs, 0, res, attrs.Length, pseudoAttrs.Length);
126126
return res;
@@ -135,13 +135,16 @@ internal static object[] GetCustomAttributes (ICustomAttributeProvider obj, Type
135135
if (obj == null)
136136
throw new ArgumentNullException (nameof (obj));
137137
if (attributeType == null)
138-
throw new ArgumentNullException (nameof (attributeType));
138+
throw new ArgumentNullException (nameof (attributeType));
139+
if (!attributeType.IsSubclassOf (typeof (Attribute)) && attributeType != typeof (Attribute)&& attributeType != typeof (CustomAttribute) && attributeType != typeof (System.Object))
140+
throw new ArgumentException (SR.Argument_MustHaveAttributeBaseClass + " " + attributeType.FullName);
139141

140142
if (attributeType == typeof (CustomAttribute))
141143
attributeType = null;
142-
143144
if (attributeType == typeof (Attribute))
144145
attributeType = null;
146+
if (attributeType == typeof (System.Object))
147+
attributeType = null;
145148

146149
object[] r;
147150
object[] res = GetCustomAttributesBase (obj, attributeType, false);
@@ -505,7 +508,9 @@ static CustomAttributeData[] GetPseudoCustomAttributesData (Type type)
505508
internal static bool IsDefined (ICustomAttributeProvider obj, Type attributeType, bool inherit)
506509
{
507510
if (attributeType == null)
508-
throw new ArgumentNullException ("attributeType");
511+
throw new ArgumentNullException (nameof (attributeType));
512+
if (!attributeType.IsSubclassOf (typeof (Attribute)) && attributeType != typeof (Attribute))
513+
throw new ArgumentException (SR.Argument_MustHaveAttributeBaseClass + " " + attributeType.FullName);
509514

510515
AttributeUsageAttribute usage = null;
511516
do {

0 commit comments

Comments
 (0)