Have a Type.IsConvertible property. (#15365)
authorJon Hanna <jon@hackcraft.net>
Tue, 5 Dec 2017 03:52:04 +0000 (03:52 +0000)
committerJan Kotas <jkotas@microsoft.com>
Tue, 5 Dec 2017 03:52:04 +0000 (19:52 -0800)
dotnet/corefx#25671 asks for this as a public API, but this just seeks
to make it available to corefx.

Motivation discussed at dotnet/corefx#25663 and dotnet/corefx#25670

Mirror PR to corert will need and overload making runtime types return
false there.

src/mscorlib/shared/System/Reflection/TypeDelegator.cs
src/mscorlib/shared/System/Type.cs
src/mscorlib/src/System/RtType.cs

index ac3972f..e0be6e8 100644 (file)
@@ -112,6 +112,9 @@ namespace System.Reflection
         protected override bool IsCOMObjectImpl() => typeImpl.IsCOMObject;
         public override bool IsByRefLike => typeImpl.IsByRefLike;
         public override bool IsConstructedGenericType => typeImpl.IsConstructedGenericType;
+
+        public override bool IsCollectible => typeImpl.IsCollectible;
+
         public override Type GetElementType() => typeImpl.GetElementType();
         protected override bool HasElementTypeImpl() => typeImpl.HasElementType;
 
index 6b6a7b3..a0d219d 100644 (file)
@@ -102,6 +102,8 @@ namespace System
         public bool IsContextful => IsContextfulImpl();
         protected virtual bool IsContextfulImpl() => false;
 
+        public virtual bool IsCollectible => true;
+
         public virtual bool IsEnum => IsSubclassOf(typeof(Enum));
         public bool IsMarshalByRef => IsMarshalByRefImpl();
         protected virtual bool IsMarshalByRefImpl() => false;
index 9e78e82..d4afc8a 100644 (file)
@@ -2459,7 +2459,7 @@ namespace System
                     IntPtr gcHandle = Interlocked.CompareExchange(ref m_cache, newgcHandle, IntPtr.Zero);
                     // Leak the handle if the type is collectible. It will be reclaimed when
                     // the type goes away.
-                    if (gcHandle != IntPtr.Zero && !IsCollectible())
+                    if (gcHandle != IntPtr.Zero && !IsCollectible)
                         GCHandle.InternalFree(newgcHandle);
                 }
 
@@ -3151,10 +3151,7 @@ namespace System
             return new RuntimeTypeHandle(this);
         }
 
-        internal bool IsCollectible()
-        {
-            return RuntimeTypeHandle.IsCollectible(GetTypeHandleInternal());
-        }
+        public sealed override bool IsCollectible => RuntimeTypeHandle.IsCollectible(GetTypeHandleInternal());
 
         protected override TypeCode GetTypeCodeImpl()
         {