Bug 39911

Summary: System.Reflection.Emit.DerivedType isn't an IReflectableType so GetTypeInfo throws
Product: [Mono] Class Libraries Reporter: Jonathon Rossi <jono>
Component: mscorlibAssignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: jono, masafa, mono-bugs+mono
Priority: ---    
Version: 4.2.0 (C6)   
Target Milestone: Untriaged   
Hardware: PC   
OS: Linux   
Tags: Is this bug a regression?: ---
Last known good build:

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)
			{
				Console.WriteLine(t.GetType().ToString());
				try
				{
					Console.WriteLine(t.GetTypeInfo().ToString());
				}
				catch (Exception ex)
				{
					Console.WriteLine(ex.Message);
				}
			}
		}
	}
}
```

.NET Framework output:
```
System.Reflection.Emit.SymbolType
T[]
System.Reflection.Emit.SymbolType
T[,]
System.Reflection.Emit.SymbolType
T&
System.Reflection.Emit.SymbolType
T*
```

Mono 4.0.2 output:
```
System.Reflection.Emit.ArrayType
Cannot cast from source type to destination type.
System.Reflection.Emit.ArrayType
Cannot cast from source type to destination type.
System.Reflection.Emit.ByRefType
Cannot cast from source type to destination type.
System.Reflection.Emit.PointerType
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.