From 441018e92ab9212515d6c3711d1e14700f9165e6 Mon Sep 17 00:00:00 2001 From: Atsushi Kanamori Date: Tue, 2 May 2017 17:03:07 -0700 Subject: [PATCH] Implement Type.IsTypeDefinition property on CoreCLR This api was just approved. https://github.com/dotnet/corefx/issues/17345 --- .../shared/System/Reflection/TypeDelegator.cs | 1 + src/mscorlib/shared/System/Type.cs | 1 + .../src/System/Reflection/Emit/EnumBuilder.cs | 2 ++ .../Emit/GenericTypeParameterBuilder.cs | 2 ++ .../src/System/Reflection/Emit/SymbolType.cs | 3 +++ .../src/System/Reflection/Emit/TypeBuilder.cs | 2 ++ .../Emit/TypeBuilderInstantiation.cs | 1 + src/mscorlib/src/System/RtType.cs | 5 +++++ src/mscorlib/src/System/RuntimeHandles.cs | 18 ++++++++++++++++++ 9 files changed, 35 insertions(+) diff --git a/src/mscorlib/shared/System/Reflection/TypeDelegator.cs b/src/mscorlib/shared/System/Reflection/TypeDelegator.cs index 7f928d2486b1..a301ddc11ea7 100644 --- a/src/mscorlib/shared/System/Reflection/TypeDelegator.cs +++ b/src/mscorlib/shared/System/Reflection/TypeDelegator.cs @@ -100,6 +100,7 @@ protected override PropertyInfo GetPropertyImpl(string name, BindingFlags bindin protected override TypeAttributes GetAttributeFlagsImpl() => typeImpl.Attributes; + public override bool IsTypeDefinition => typeImpl.IsTypeDefinition; public override bool IsSZArray => typeImpl.IsSZArray; protected override bool IsArrayImpl() => typeImpl.IsArray; diff --git a/src/mscorlib/shared/System/Type.cs b/src/mscorlib/shared/System/Type.cs index 7749c1741410..2ba58918a02a 100644 --- a/src/mscorlib/shared/System/Type.cs +++ b/src/mscorlib/shared/System/Type.cs @@ -32,6 +32,7 @@ protected Type() { } public override Type ReflectedType => null; public abstract Type UnderlyingSystemType { get; } + public virtual bool IsTypeDefinition { get { throw NotImplemented.ByDesign; } } public bool IsArray => IsArrayImpl(); protected abstract bool IsArrayImpl(); public bool IsByRef => IsByRefImpl(); diff --git a/src/mscorlib/src/System/Reflection/Emit/EnumBuilder.cs b/src/mscorlib/src/System/Reflection/Emit/EnumBuilder.cs index 55aa5c5a8fa6..a36882b03632 100644 --- a/src/mscorlib/src/System/Reflection/Emit/EnumBuilder.cs +++ b/src/mscorlib/src/System/Reflection/Emit/EnumBuilder.cs @@ -242,6 +242,8 @@ protected override TypeAttributes GetAttributeFlagsImpl() return m_typeBuilder.Attributes; } + public override bool IsTypeDefinition => true; + public override bool IsSZArray => false; protected override bool IsArrayImpl() diff --git a/src/mscorlib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs b/src/mscorlib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs index dd5ffa92a979..75e4acc9030a 100644 --- a/src/mscorlib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs +++ b/src/mscorlib/src/System/Reflection/Emit/GenericTypeParameterBuilder.cs @@ -154,6 +154,8 @@ public override Type MakeArrayType(int rank) protected override TypeAttributes GetAttributeFlagsImpl() { return TypeAttributes.Public; } + public override bool IsTypeDefinition => false; + public override bool IsSZArray => false; protected override bool IsArrayImpl() { return false; } diff --git a/src/mscorlib/src/System/Reflection/Emit/SymbolType.cs b/src/mscorlib/src/System/Reflection/Emit/SymbolType.cs index 16848b43dda5..58d5a17f77a8 100644 --- a/src/mscorlib/src/System/Reflection/Emit/SymbolType.cs +++ b/src/mscorlib/src/System/Reflection/Emit/SymbolType.cs @@ -271,6 +271,9 @@ internal void SetFormat(string format, int curIndex, int length) #endregion #region Type Overrides + + public override bool IsTypeDefinition => false; + public override bool IsSZArray => m_cRank <= 1 && m_isSzArray; public override Type MakePointerType() diff --git a/src/mscorlib/src/System/Reflection/Emit/TypeBuilder.cs b/src/mscorlib/src/System/Reflection/Emit/TypeBuilder.cs index a98af2bdcff4..5ccbf34f5999 100644 --- a/src/mscorlib/src/System/Reflection/Emit/TypeBuilder.cs +++ b/src/mscorlib/src/System/Reflection/Emit/TypeBuilder.cs @@ -1109,6 +1109,8 @@ protected override TypeAttributes GetAttributeFlagsImpl() return m_iAttr; } + public override bool IsTypeDefinition => true; + public override bool IsSZArray => false; protected override bool IsArrayImpl() diff --git a/src/mscorlib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/mscorlib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs index 6d46362f917f..64a38b0995ea 100644 --- a/src/mscorlib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs +++ b/src/mscorlib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs @@ -189,6 +189,7 @@ public override Type BaseType public override MemberInfo[] GetMembers(BindingFlags bindingAttr) { throw new NotSupportedException(); } protected override TypeAttributes GetAttributeFlagsImpl() { return m_type.Attributes; } + public override bool IsTypeDefinition => false; public override bool IsSZArray => false; protected override bool IsArrayImpl() { return false; } diff --git a/src/mscorlib/src/System/RtType.cs b/src/mscorlib/src/System/RtType.cs index ef3ba2978762..6d0aa6fe1d2e 100644 --- a/src/mscorlib/src/System/RtType.cs +++ b/src/mscorlib/src/System/RtType.cs @@ -3792,6 +3792,11 @@ public override Type[] GetGenericParameterConstraints() #endregion #region Misc + public override bool IsTypeDefinition + { + get { return RuntimeTypeHandle.IsTypeDefinition(this); } + } + public override Type MakePointerType() { return new RuntimeTypeHandle(this).MakePointer(); } public override Type MakeByRefType() { return new RuntimeTypeHandle(this).MakeByRef(); } public override Type MakeArrayType() { return new RuntimeTypeHandle(this).MakeSZArray(); } diff --git a/src/mscorlib/src/System/RuntimeHandles.cs b/src/mscorlib/src/System/RuntimeHandles.cs index ab125e741e35..fd32547af773 100644 --- a/src/mscorlib/src/System/RuntimeHandles.cs +++ b/src/mscorlib/src/System/RuntimeHandles.cs @@ -127,6 +127,24 @@ internal RuntimeTypeHandle(RuntimeType type) m_type = type; } + internal static bool IsTypeDefinition(RuntimeType type) + { + CorElementType corElemType = GetCorElementType(type); + if (!((corElemType >= CorElementType.Void && corElemType < CorElementType.Ptr) || + corElemType == CorElementType.ValueType || + corElemType == CorElementType.Class || + corElemType == CorElementType.TypedByRef || + corElemType == CorElementType.I || + corElemType == CorElementType.U || + corElemType == CorElementType.Object)) + return false; + + if (HasInstantiation(type) && !IsGenericTypeDefinition(type)) + return false; + + return true; + } + internal static bool IsPrimitive(RuntimeType type) { CorElementType corElemType = GetCorElementType(type);