Bug 39911 - System.Reflection.Emit.DerivedType isn't an IReflectableType so GetTypeInfo throws
Summary: System.Reflection.Emit.DerivedType isn't an IReflectableType so GetTypeInfo t...
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: 4.2.0 (C6)
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Marek Safar
Depends on:
Reported: 2016-03-25 16:06 UTC by Jonathon Rossi
Modified: 2016-04-13 12:36 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Jonathon Rossi 2016-03-25 16:06:53 UTC
GenericTypeParameterBuilder.MakeArrayType() returns a System.Reflection.Emit.ArrayType (internal type) which inherits from DerivedType (also an internal type) which inherits from System.Type. MakeArrayType's signature returns System.Type which then allows you to call the GetTypeInfo() extension method to read properties like IsValueType (and all the others that have been moved out of Type especially with the old methods gone in .NET Core).

The problem is that GetTypeInfo throws a "System.InvalidCastException : Specified cast is not valid." because DerivedType doesn't implement IReflectableType.

On .NET Framework the implementation is as:
* RuntimeType -> TypeInfo -> Type -> MemberInfo
                          -> IReflectableType
* SymbolType  -> TypeInfo -> Type -> MemberInfo
                          -> IReflectableType

On Mono the implementation doesn't have the internal SymbolType but one class for each of array, byref and pointer types:
* MonoType -> RuntimeType -> TypeInfo -> Type -> MemberInfo
                                      -> IReflectableType
* ArrayType -> DerivedType            -> Type -> MemberInfo

It appears as though TypeInfo wasn't pushed into the hierarchy of DerivedType when it was added to RuntimeType in Mono, which results in the GetTypeInfo extension method falling over because it assumes all publicly accessible System.Types implement IReflectableType.

Below is a unit test to reproduce this, there are a couple more things I've written in the issue for Castle DynamicProxy in our issue tracker which may or may not help: https://github.com/castleproject/Core/issues/160#issuecomment-199788363

Here is a unit test:
using System;
using System.Reflection;
using System.Reflection.Emit;

namespace ConsoleApplication3
	public class Program
		public static void Main(string[] args)
			AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("TestDynAssembly"), AssemblyBuilderAccess.Run);
			ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule("TestDynModule");
			TypeBuilder typeBuilder = moduleBuilder.DefineType("TestDynType", TypeAttributes.Public);
			GenericTypeParameterBuilder[] typeParamBuilders = typeBuilder.DefineGenericParameters(new[] { "T" });

			Type arrayType = typeParamBuilders[0].MakeArrayType();
			Type twoDimArrayType = typeParamBuilders[0].MakeArrayType(2);
			Type byRefType = typeParamBuilders[0].MakeByRefType();
			Type pointerType = typeParamBuilders[0].MakePointerType();

			Type[] parameterTypes = { arrayType, twoDimArrayType, byRefType, pointerType };
			foreach (Type t in parameterTypes)
				catch (Exception ex)

.NET Framework output:

Mono 4.0.2 output:
Cannot cast from source type to destination type.
Cannot cast from source type to destination type.
Cannot cast from source type to destination type.
Cannot cast from source type to destination type.
Comment 1 Marek Safar 2016-04-12 13:23:22 UTC
Fixed in master
Comment 2 Jonathon Rossi 2016-04-13 12:36:01 UTC
Many thanks Marek, I suspected you'd bring in more referencesource here.