From e9bdc213b3a26c6b52ec37d119965e2ba41ae360 Mon Sep 17 00:00:00 2001 From: Atsushi Kanamori Date: Tue, 4 Sep 2018 07:13:03 -0700 Subject: [PATCH 1/3] Virtual AttributeType property and signature generic types https://github.com/dotnet/corefx/issues/31614 1. This will allow Reflection providers the option to supply the attribute type without building an entire constructor. https://github.com/dotnet/corefx/issues/31798 2. This will permit other Reflection providers to support Type.MakeGenericMethodParameter() in their implementations. --- src/System.Private.CoreLib/shared/System/Type.cs | 15 +++++++++++++++ .../src/System/Reflection/CustomAttribute.cs | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/System.Private.CoreLib/shared/System/Type.cs b/src/System.Private.CoreLib/shared/System/Type.cs index 79f6b6f2f9f9..0ac5f4e2a1ba 100644 --- a/src/System.Private.CoreLib/shared/System/Type.cs +++ b/src/System.Private.CoreLib/shared/System/Type.cs @@ -346,6 +346,21 @@ public virtual Array GetEnumValues() public virtual Type MakeGenericType(params Type[] typeArguments) { throw new NotSupportedException(SR.NotSupported_SubclassOverride); } public virtual Type MakePointerType() { throw new NotSupportedException(); } + public static Type MakeGenericSignatureType(Type genericTypeDefinition, params Type[] typeArguments) + { + if (genericTypeDefinition == null) + throw new ArgumentNullException(nameof(genericTypeDefinition)); + if (typeArguments == null) + throw new ArgumentNullException(nameof(typeArguments)); + for (int i = 0; i < typeArguments.Length; i++) + { + if (typeArguments[i] == null) + throw new ArgumentNullException(); + } + + return new SignatureConstructedGenericType(genericTypeDefinition, typeArguments); + } + public static Type MakeGenericMethodParameter(int position) { if (position < 0) diff --git a/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs b/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs index 767fadf41171..c1c6e4f033d1 100644 --- a/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs +++ b/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs @@ -500,7 +500,7 @@ public override bool Equals(object obj) #endregion #region Public Members - public Type AttributeType { get { return Constructor.DeclaringType; } } + public virtual Type AttributeType { get { return Constructor.DeclaringType; } } public virtual ConstructorInfo Constructor { get { return m_ctor; } } From 35381934dc845d9ea88c18323f9717140b4a817f Mon Sep 17 00:00:00 2001 From: Atsushi Kanamori Date: Tue, 4 Sep 2018 08:54:12 -0700 Subject: [PATCH 2/3] More robust argument validation. --- .../SignatureConstructedGenericType.cs | 16 ++++++++++++++-- src/System.Private.CoreLib/shared/System/Type.cs | 15 +-------------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Reflection/SignatureConstructedGenericType.cs b/src/System.Private.CoreLib/shared/System/Reflection/SignatureConstructedGenericType.cs index cd97ffa21b10..8bf36ab05cf5 100644 --- a/src/System.Private.CoreLib/shared/System/Reflection/SignatureConstructedGenericType.cs +++ b/src/System.Private.CoreLib/shared/System/Reflection/SignatureConstructedGenericType.cs @@ -12,9 +12,21 @@ internal sealed class SignatureConstructedGenericType : SignatureType { internal SignatureConstructedGenericType(Type genericTypeDefinition, Type[] genericTypeArguments) { - Debug.Assert(genericTypeDefinition != null && genericTypeArguments != null); + if (genericTypeDefinition == null) + throw new ArgumentNullException(nameof(genericTypeDefinition)); + + if (genericTypeArguments == null) + throw new ArgumentNullException(nameof(genericTypeArguments)); + + genericTypeArguments = (Type[])(genericTypeArguments.Clone()); + for (int i = 0; i < genericTypeArguments.Length; i++) + { + if (genericTypeArguments[i] == null) + throw new ArgumentNullException(nameof(genericTypeArguments)); + } + _genericTypeDefinition = genericTypeDefinition; - _genericTypeArguments = (Type[])(genericTypeArguments.Clone()); + _genericTypeArguments = genericTypeArguments; } public sealed override bool IsTypeDefinition => false; diff --git a/src/System.Private.CoreLib/shared/System/Type.cs b/src/System.Private.CoreLib/shared/System/Type.cs index 0ac5f4e2a1ba..c78d98833a98 100644 --- a/src/System.Private.CoreLib/shared/System/Type.cs +++ b/src/System.Private.CoreLib/shared/System/Type.cs @@ -346,20 +346,7 @@ public virtual Array GetEnumValues() public virtual Type MakeGenericType(params Type[] typeArguments) { throw new NotSupportedException(SR.NotSupported_SubclassOverride); } public virtual Type MakePointerType() { throw new NotSupportedException(); } - public static Type MakeGenericSignatureType(Type genericTypeDefinition, params Type[] typeArguments) - { - if (genericTypeDefinition == null) - throw new ArgumentNullException(nameof(genericTypeDefinition)); - if (typeArguments == null) - throw new ArgumentNullException(nameof(typeArguments)); - for (int i = 0; i < typeArguments.Length; i++) - { - if (typeArguments[i] == null) - throw new ArgumentNullException(); - } - - return new SignatureConstructedGenericType(genericTypeDefinition, typeArguments); - } + public static Type MakeGenericSignatureType(Type genericTypeDefinition, params Type[] typeArguments) => new SignatureConstructedGenericType(genericTypeDefinition, typeArguments); public static Type MakeGenericMethodParameter(int position) { From 7fdf8b52a939b9b31e56d890af77da5aa4f89db8 Mon Sep 17 00:00:00 2001 From: Atsushi Kanamori Date: Tue, 4 Sep 2018 09:08:44 -0700 Subject: [PATCH 3/3] Change parameter name --- .../SignatureConstructedGenericType.cs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/System.Private.CoreLib/shared/System/Reflection/SignatureConstructedGenericType.cs b/src/System.Private.CoreLib/shared/System/Reflection/SignatureConstructedGenericType.cs index 8bf36ab05cf5..d3d88520fc5f 100644 --- a/src/System.Private.CoreLib/shared/System/Reflection/SignatureConstructedGenericType.cs +++ b/src/System.Private.CoreLib/shared/System/Reflection/SignatureConstructedGenericType.cs @@ -10,23 +10,25 @@ namespace System.Reflection { internal sealed class SignatureConstructedGenericType : SignatureType { - internal SignatureConstructedGenericType(Type genericTypeDefinition, Type[] genericTypeArguments) + // The exception-visible name "typeArguments" is chosen to match the parameter name to Type.MakeGenericType() since that's the + // intended user of this constructor. + internal SignatureConstructedGenericType(Type genericTypeDefinition, Type[] typeArguments) { if (genericTypeDefinition == null) throw new ArgumentNullException(nameof(genericTypeDefinition)); - if (genericTypeArguments == null) - throw new ArgumentNullException(nameof(genericTypeArguments)); + if (typeArguments == null) + throw new ArgumentNullException(nameof(typeArguments)); - genericTypeArguments = (Type[])(genericTypeArguments.Clone()); - for (int i = 0; i < genericTypeArguments.Length; i++) + typeArguments = (Type[])(typeArguments.Clone()); + for (int i = 0; i < typeArguments.Length; i++) { - if (genericTypeArguments[i] == null) - throw new ArgumentNullException(nameof(genericTypeArguments)); + if (typeArguments[i] == null) + throw new ArgumentNullException(nameof(typeArguments)); } _genericTypeDefinition = genericTypeDefinition; - _genericTypeArguments = genericTypeArguments; + _genericTypeArguments = typeArguments; } public sealed override bool IsTypeDefinition => false;