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

3 files changed

Lines changed: 27 additions & 51 deletions

File tree

‎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
publicstaticAttribute[]GetCustomAttributes(Assemblyelement)=>(Attribute[])CustomAttribute.GetCustomAttributes(element,true);
3737
publicstaticAttribute[]GetCustomAttributes(Assemblyelement,boolinherit)=>(Attribute[])CustomAttribute.GetCustomAttributes(element,inherit);
38-
publicstaticAttribute[]GetCustomAttributes(Assemblyelement,TypeattributeType)=>(Attribute[])GetCustomAttributes((ICustomAttributeProvider)element,attributeType,true);
39-
publicstaticAttribute[]GetCustomAttributes(Assemblyelement,TypeattributeType,boolinherit)=>(Attribute[])GetCustomAttributes((ICustomAttributeProvider)element,attributeType,inherit);
38+
publicstaticAttribute[]GetCustomAttributes(Assemblyelement,TypeattributeType)=>(Attribute[])CustomAttribute.GetCustomAttributes((ICustomAttributeProvider)element,attributeType,true);
39+
publicstaticAttribute[]GetCustomAttributes(Assemblyelement,TypeattributeType,boolinherit)=>(Attribute[])CustomAttribute.GetCustomAttributes((ICustomAttributeProvider)element,attributeType,inherit);
4040
publicstaticAttribute[]GetCustomAttributes(MemberInfoelement)=>(Attribute[])CustomAttribute.GetCustomAttributes(element,true);
4141
publicstaticAttribute[]GetCustomAttributes(MemberInfoelement,boolinherit)=>(Attribute[])CustomAttribute.GetCustomAttributes(element,inherit);
42-
publicstaticAttribute[]GetCustomAttributes(MemberInfoelement,TypeattributeType)=>(Attribute[])GetCustomAttributes((ICustomAttributeProvider)element,attributeType,true);
43-
publicstaticAttribute[]GetCustomAttributes(MemberInfoelement,TypeattributeType,boolinherit)=>(Attribute[])GetCustomAttributes((ICustomAttributeProvider)element,attributeType,inherit);
42+
publicstaticAttribute[]GetCustomAttributes(MemberInfoelement,TypeattributeType)=>(Attribute[])CustomAttribute.GetCustomAttributes((ICustomAttributeProvider)element,attributeType,true);
43+
publicstaticAttribute[]GetCustomAttributes(MemberInfoelement,TypeattributeType,boolinherit)=>(Attribute[])CustomAttribute.GetCustomAttributes((ICustomAttributeProvider)element,attributeType,inherit);
4444
publicstaticAttribute[]GetCustomAttributes(Moduleelement)=>(Attribute[])CustomAttribute.GetCustomAttributes(element,true);
4545
publicstaticAttribute[]GetCustomAttributes(Moduleelement,boolinherit)=>(Attribute[])CustomAttribute.GetCustomAttributes(element,inherit);
46-
publicstaticAttribute[]GetCustomAttributes(Moduleelement,TypeattributeType)=>(Attribute[])GetCustomAttributes((ICustomAttributeProvider)element,attributeType,true);
47-
publicstaticAttribute[]GetCustomAttributes(Moduleelement,TypeattributeType,boolinherit)=>(Attribute[])GetCustomAttributes((ICustomAttributeProvider)element,attributeType,inherit);
46+
publicstaticAttribute[]GetCustomAttributes(Moduleelement,TypeattributeType)=>(Attribute[])CustomAttribute.GetCustomAttributes((ICustomAttributeProvider)element,attributeType,true);
47+
publicstaticAttribute[]GetCustomAttributes(Moduleelement,TypeattributeType,boolinherit)=>(Attribute[])CustomAttribute.GetCustomAttributes((ICustomAttributeProvider)element,attributeType,inherit);
4848
publicstaticAttribute[]GetCustomAttributes(ParameterInfoelement)=>(Attribute[])CustomAttribute.GetCustomAttributes(element,true);
4949
publicstaticAttribute[]GetCustomAttributes(ParameterInfoelement,boolinherit)=>(Attribute[])CustomAttribute.GetCustomAttributes(element,inherit);
50-
publicstaticAttribute[]GetCustomAttributes(ParameterInfoelement,TypeattributeType)=>(Attribute[])GetCustomAttributes((ICustomAttributeProvider)element,attributeType,true);
51-
publicstaticAttribute[]GetCustomAttributes(ParameterInfoelement,TypeattributeType,boolinherit)=>(Attribute[])GetCustomAttributes((ICustomAttributeProvider)element,attributeType,inherit);
52-
53-
internalstaticAttribute[]GetCustomAttributes(ICustomAttributeProviderelement,TypeattributeType,boolinherit)
54-
{
55-
if(attributeType==null)
56-
thrownewArgumentNullException(nameof(attributeType));
57-
if(!attributeType.IsSubclassOf(typeof(Attribute))&&attributeType!=typeof(Attribute))
58-
thrownewArgumentException(SR.Argument_MustHaveAttributeBaseClass+" "+attributeType.FullName);
59-
60-
return(Attribute[])CustomAttribute.GetCustomAttributes(element,attributeType,inherit);
61-
}
62-
63-
publicstaticboolIsDefined(Assemblyelement,TypeattributeType)=>IsDefined((ICustomAttributeProvider)element,attributeType,true);
64-
publicstaticboolIsDefined(Assemblyelement,TypeattributeType,boolinherit)=>IsDefined((ICustomAttributeProvider)element,attributeType,inherit);
65-
publicstaticboolIsDefined(MemberInfoelement,TypeattributeType)=>IsDefined((ICustomAttributeProvider)element,attributeType,true);
66-
publicstaticboolIsDefined(MemberInfoelement,TypeattributeType,boolinherit)=>IsDefined((ICustomAttributeProvider)element,attributeType,inherit);
67-
publicstaticboolIsDefined(Moduleelement,TypeattributeType)=>IsDefined((ICustomAttributeProvider)element,attributeType,true);
68-
publicstaticboolIsDefined(Moduleelement,TypeattributeType,boolinherit)=>IsDefined((ICustomAttributeProvider)element,attributeType,inherit);
69-
publicstaticboolIsDefined(ParameterInfoelement,TypeattributeType)=>IsDefined((ICustomAttributeProvider)element,attributeType,true);
70-
publicstaticboolIsDefined(ParameterInfoelement,TypeattributeType,boolinherit)=>IsDefined((ICustomAttributeProvider)element,attributeType,inherit);
71-
72-
internalstaticboolIsDefined(ICustomAttributeProviderelement,TypeattributeType,boolinherit)
73-
{
74-
if(attributeType==null)
75-
thrownewArgumentNullException(nameof(attributeType));
76-
if(!attributeType.IsSubclassOf(typeof(Attribute))&&attributeType!=typeof(Attribute))
77-
thrownewArgumentException(SR.Argument_MustHaveAttributeBaseClass+" "+attributeType.FullName);
78-
79-
returnCustomAttribute.IsDefined(element,attributeType,inherit);
80-
}
50+
publicstaticAttribute[]GetCustomAttributes(ParameterInfoelement,TypeattributeType)=>(Attribute[])CustomAttribute.GetCustomAttributes((ICustomAttributeProvider)element,attributeType,true);
51+
publicstaticAttribute[]GetCustomAttributes(ParameterInfoelement,TypeattributeType,boolinherit)=>(Attribute[])CustomAttribute.GetCustomAttributes((ICustomAttributeProvider)element,attributeType,inherit);
52+
53+
publicstaticboolIsDefined(Assemblyelement,TypeattributeType)=>CustomAttribute.IsDefined((ICustomAttributeProvider)element,attributeType,true);
54+
publicstaticboolIsDefined(Assemblyelement,TypeattributeType,boolinherit)=>CustomAttribute.IsDefined((ICustomAttributeProvider)element,attributeType,inherit);
55+
publicstaticboolIsDefined(MemberInfoelement,TypeattributeType)=>CustomAttribute.IsDefined((ICustomAttributeProvider)element,attributeType,true);
56+
publicstaticboolIsDefined(MemberInfoelement,TypeattributeType,boolinherit)=>CustomAttribute.IsDefined((ICustomAttributeProvider)element,attributeType,inherit);
57+
publicstaticboolIsDefined(Moduleelement,TypeattributeType)=>CustomAttribute.IsDefined((ICustomAttributeProvider)element,attributeType,true);
58+
publicstaticboolIsDefined(Moduleelement,TypeattributeType,boolinherit)=>CustomAttribute.IsDefined((ICustomAttributeProvider)element,attributeType,inherit);
59+
publicstaticboolIsDefined(ParameterInfoelement,TypeattributeType)=>CustomAttribute.IsDefined((ICustomAttributeProvider)element,attributeType,true);
60+
publicstaticboolIsDefined(ParameterInfoelement,TypeattributeType,boolinherit)=>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-
internalstaticexternobject[]GetCustomAttributesInternal(ICustomAttributeProviderobj,TypeattributeType,boolpseudoAttrs);
56+
internalstaticexternAttribute[]GetCustomAttributesInternal(ICustomAttributeProviderobj,TypeattributeType,boolpseudoAttrs);
5757

5858
internalstaticobject[]GetPseudoCustomAttributes(ICustomAttributeProviderobj,TypeattributeType){
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=newobject[attrs.Length+pseudoAttrs.Length];
123+
object[]res=newAttribute[attrs.Length+pseudoAttrs.Length];
124124
System.Array.Copy(attrs,res,attrs.Length);
125125
System.Array.Copy(pseudoAttrs,0,res,attrs.Length,pseudoAttrs.Length);
126126
returnres;
@@ -135,13 +135,16 @@ internal static object[] GetCustomAttributes (ICustomAttributeProvider obj, Type
135135
if(obj==null)
136136
thrownewArgumentNullException(nameof(obj));
137137
if(attributeType==null)
138-
thrownewArgumentNullException(nameof(attributeType));
138+
thrownewArgumentNullException(nameof(attributeType));
139+
if(!attributeType.IsSubclassOf(typeof(Attribute))&&attributeType!=typeof(Attribute)&&attributeType!=typeof(CustomAttribute)&&attributeType!=typeof(System.Object))
140+
thrownewArgumentException(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
internalstaticboolIsDefined(ICustomAttributeProviderobj,TypeattributeType,boolinherit)
506509
{
507510
if(attributeType==null)
508-
thrownewArgumentNullException("attributeType");
511+
thrownewArgumentNullException(nameof(attributeType));
512+
if(!attributeType.IsSubclassOf(typeof(Attribute))&&attributeType!=typeof(Attribute))
513+
thrownewArgumentException(SR.Argument_MustHaveAttributeBaseClass+" "+attributeType.FullName);
509514

510515
AttributeUsageAttributeusage=null;
511516
do{

0 commit comments

Comments
 (0)