Bugzilla – Bug 320300
[GENERICS] Runtime doesn't check constraints
Last modified: 2010-11-27 19:05:04 UTC
---- Reported by martin@ximian.com 2006-02-10 04:24:55 MST ---- Our runtime doesn't check the constraints when instantiating a generic type. For instance in the following example, Type.MakeGenericType() must throw an ArgumentException. ===== using System; public class Foo<T> where T : struct { } class X { static void Main () { Type t = typeof (Foo<>); t.MakeGenericType (typeof (X)); } } ===== On Windows, this is throwing at runtime: ===== martin@mordor ~ $ csc N.cs Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.7 for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727 Copyright (C) Microsoft Corporation 2001-2005. All rights reserved. martin@mordor ~ $ ./N.exe Unhandled Exception: System.ArgumentException: GenericArguments[0], 'X', on 'Foo`1[T]' violates the constraint of type 'T'. ---> System.TypeLoadException: GenericArguments[0], 'X', on 'Foo`1[T]' violates the constraint of type parameter 'T'. at System.RuntimeTypeHandle._Instantiate(RuntimeTypeHandle[] inst) at System.RuntimeTypeHandle.Instantiate(RuntimeTypeHandle[] inst) at System.RuntimeType.MakeGenericType(Type[] instantiation) --- End of inner exception stack trace --- at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, Type[] genericArguments, Exception e) at System.RuntimeType.MakeGenericType(Type[] instantiation) at X.Main() martin@mordor ~ =============== ---- Additional Comments From miguel@ximian.com 2006-10-07 11:37:22 MST ---- Moving to runtime Unknown operating system unknown. Setting to default OS "Other".
Checked 2.6.4, no progress after almost 4.5 years. Any plans to fix?
Done.