From f0bb47144d8579b2763c6c3eae0320a422c29f96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Wed, 25 Nov 2026 14:05:55 +0000 Subject: [PATCH 1/2] Add MemberAccessException and MissingMethodException - Add MemberAccessException, MissingMemberException and MissingMethodException. - Activator.CreateInstance throws MissingMethodException. - Update tests. Co-Authored-By: Claude Sonnet 5.5 --- .../UnitTestActivatorTests.cs | 20 ++++----- nanoFramework.CoreLibrary/CoreLibrary.nfproj | 3 ++ nanoFramework.CoreLibrary/System/Activator.cs | 4 +- .../System/MemberAccessException.cs | 42 +++++++++++++++++++ .../System/MissingMemberException.cs | 42 +++++++++++++++++++ .../System/MissingMethodException.cs | 42 +++++++++++++++++++ 6 files changed, 141 insertions(+), 12 deletions(-) create mode 100644 nanoFramework.CoreLibrary/System/MemberAccessException.cs create mode 100644 nanoFramework.CoreLibrary/System/MissingMemberException.cs create mode 100644 nanoFramework.CoreLibrary/System/MissingMethodException.cs diff --git a/Tests/NFUnitTestSystemLibReflection3/UnitTestActivatorTests.cs b/Tests/NFUnitTestSystemLibReflection3/UnitTestActivatorTests.cs index d1d36e88..37429e78 100644 --- a/Tests/NFUnitTestSystemLibReflection3/UnitTestActivatorTests.cs +++ b/Tests/NFUnitTestSystemLibReflection3/UnitTestActivatorTests.cs @@ -165,16 +165,16 @@ public void CreateInstanceOfT_ReturnsTheTypedInstance() } [TestMethod] - public void CreateInstanceOfT_WithoutAPublicParameterlessConstructor_ThrowsNotSupportedException_Deviation() - { - Assert.ThrowsException(typeof(NotSupportedException), () => Activator.CreateInstance()); - Assert.ThrowsException(typeof(NotSupportedException), () => Activator.CreateInstance()); - Assert.ThrowsException(typeof(NotSupportedException), () => Activator.CreateInstance()); - Assert.ThrowsException(typeof(NotSupportedException), () => Activator.CreateInstance()); - Assert.ThrowsException(typeof(NotSupportedException), () => Activator.CreateInstance()); - Assert.ThrowsException(typeof(NotSupportedException), () => Activator.CreateInstance()); - Assert.ThrowsException(typeof(NotSupportedException), () => Activator.CreateInstance()); - Assert.ThrowsException(typeof(NotSupportedException), () => Activator.CreateInstance()); + public void CreateInstanceOfT_WithoutAPublicParameterlessConstructor_ThrowsMissingMethodException_Deviation() + { + Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance()); + Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance()); + Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance()); + Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance()); + Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance()); + Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance()); + Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance()); + Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance()); } [TestMethod] diff --git a/nanoFramework.CoreLibrary/CoreLibrary.nfproj b/nanoFramework.CoreLibrary/CoreLibrary.nfproj index f2ecc359..fe5c0fe8 100644 --- a/nanoFramework.CoreLibrary/CoreLibrary.nfproj +++ b/nanoFramework.CoreLibrary/CoreLibrary.nfproj @@ -145,6 +145,9 @@ + + + diff --git a/nanoFramework.CoreLibrary/System/Activator.cs b/nanoFramework.CoreLibrary/System/Activator.cs index 12477ab3..9549f8d1 100644 --- a/nanoFramework.CoreLibrary/System/Activator.cs +++ b/nanoFramework.CoreLibrary/System/Activator.cs @@ -17,7 +17,7 @@ public static class Activator /// /// The type to create. /// A reference to the newly created object, or the default value for a value type. - /// The type is abstract or has no public parameterless constructor. + /// The type is abstract or has no public parameterless constructor. public static T CreateInstance() { Type type = typeof(T); @@ -31,7 +31,7 @@ public static T CreateInstance() if (constructor == null || !constructor.IsPublic) { - throw new NotSupportedException(); + throw new MissingMethodException(); } return (T)constructor.Invoke(null); diff --git a/nanoFramework.CoreLibrary/System/MemberAccessException.cs b/nanoFramework.CoreLibrary/System/MemberAccessException.cs new file mode 100644 index 00000000..7ef29536 --- /dev/null +++ b/nanoFramework.CoreLibrary/System/MemberAccessException.cs @@ -0,0 +1,42 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#if NANOCLR_REFLECTION + +namespace System +{ + /// + /// The exception that is thrown when an attempt to access a class member fails. + /// + [Serializable] + public class MemberAccessException : SystemException + { + /// + /// Initializes a new instance of the class. + /// + public MemberAccessException() + { + } + + /// + /// Initializes a new instance of the class with the specified error message. + /// + /// The message that describes the error. + public MemberAccessException(string message) + : base(message) + { + } + + /// + /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. + /// + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception. + public MemberAccessException(string message, Exception innerException) + : base(message, innerException) + { + } + } +} + +#endif // NANOCLR_REFLECTION diff --git a/nanoFramework.CoreLibrary/System/MissingMemberException.cs b/nanoFramework.CoreLibrary/System/MissingMemberException.cs new file mode 100644 index 00000000..ae3aaede --- /dev/null +++ b/nanoFramework.CoreLibrary/System/MissingMemberException.cs @@ -0,0 +1,42 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#if NANOCLR_REFLECTION + +namespace System +{ + /// + /// The exception that is thrown when there is an attempt to dynamically access a class member that does not exist. + /// + [Serializable] + public class MissingMemberException : MemberAccessException + { + /// + /// Initializes a new instance of the class. + /// + public MissingMemberException() + { + } + + /// + /// Initializes a new instance of the class with the specified error message. + /// + /// The message that describes the error. + public MissingMemberException(string message) + : base(message) + { + } + + /// + /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. + /// + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception. + public MissingMemberException(string message, Exception innerException) + : base(message, innerException) + { + } + } +} + +#endif // NANOCLR_REFLECTION diff --git a/nanoFramework.CoreLibrary/System/MissingMethodException.cs b/nanoFramework.CoreLibrary/System/MissingMethodException.cs new file mode 100644 index 00000000..55c22641 --- /dev/null +++ b/nanoFramework.CoreLibrary/System/MissingMethodException.cs @@ -0,0 +1,42 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +#if NANOCLR_REFLECTION + +namespace System +{ + /// + /// The exception that is thrown when there is an attempt to dynamically access a method that does not exist. + /// + [Serializable] + public class MissingMethodException : MissingMemberException + { + /// + /// Initializes a new instance of the class. + /// + public MissingMethodException() + { + } + + /// + /// Initializes a new instance of the class with the specified error message. + /// + /// The message that describes the error. + public MissingMethodException(string message) + : base(message) + { + } + + /// + /// Initializes a new instance of the class with a specified error message and a reference to the inner exception that is the cause of this exception. + /// + /// The error message that explains the reason for the exception. + /// The exception that is the cause of the current exception. + public MissingMethodException(string message, Exception innerException) + : base(message, innerException) + { + } + } +} + +#endif // NANOCLR_REFLECTION From 82ed4f1444e29d8e0ac4340ebcfb20e718df27a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Wed, 25 Nov 2026 14:32:02 +0000 Subject: [PATCH 2/2] Add tests for constructor reflection and generic limits - Add tests for the public constructor lookup and for abstract and open generic types. - Add tests for casts at and over the generic node and nesting limits. Co-Authored-By: Claude Sonnet 5.5 --- .../NFUnitTestSystemLibReflection3.nfproj | 2 + .../UnitTestConstructorVisibilityTests.cs | 182 ++++++++++++++++++ .../UnitTestGenericLimitTests.cs | 126 ++++++++++++ 3 files changed, 310 insertions(+) create mode 100644 Tests/NFUnitTestSystemLibReflection3/UnitTestConstructorVisibilityTests.cs create mode 100644 Tests/NFUnitTestSystemLibReflection3/UnitTestGenericLimitTests.cs diff --git a/Tests/NFUnitTestSystemLibReflection3/NFUnitTestSystemLibReflection3.nfproj b/Tests/NFUnitTestSystemLibReflection3/NFUnitTestSystemLibReflection3.nfproj index 1742aae3..abd37075 100644 --- a/Tests/NFUnitTestSystemLibReflection3/NFUnitTestSystemLibReflection3.nfproj +++ b/Tests/NFUnitTestSystemLibReflection3/NFUnitTestSystemLibReflection3.nfproj @@ -33,6 +33,8 @@ + + diff --git a/Tests/NFUnitTestSystemLibReflection3/UnitTestConstructorVisibilityTests.cs b/Tests/NFUnitTestSystemLibReflection3/UnitTestConstructorVisibilityTests.cs new file mode 100644 index 00000000..667f89c1 --- /dev/null +++ b/Tests/NFUnitTestSystemLibReflection3/UnitTestConstructorVisibilityTests.cs @@ -0,0 +1,182 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Reflection; +using nanoFramework.TestFramework; + +namespace NFUnitTestSystemLibReflection3 +{ + public class ClassWithPublicConstructor + { + public int X = 1; + } + + public class ClassWithPrivateConstructors + { + public int X; + + private ClassWithPrivateConstructors() + { + X = 2; + } + + private ClassWithPrivateConstructors(int x) + { + X = x; + } + } + + public class ClassWithProtectedConstructor + { + protected ClassWithProtectedConstructor() + { + } + } + + public class ClassWithInternalConstructor + { + internal ClassWithInternalConstructor() + { + } + } + + public class ClassWithMixedConstructors + { + public ClassWithMixedConstructors() + { + } + + public ClassWithMixedConstructors(char c) + { + } + + protected ClassWithMixedConstructors(string s) + { + } + + internal ClassWithMixedConstructors(long l) + { + } + + private ClassWithMixedConstructors(int i) + { + } + } + + public abstract class AbstractClassWithPublicConstructor + { + public AbstractClassWithPublicConstructor() + { + } + } + + public abstract class AbstractGenericClass + { + public AbstractGenericClass() + { + } + } + + public class GenericClassWithPublicConstructor + { + public GenericClassWithPublicConstructor() + { + } + } + + public interface IMarkerInterface + { + } + + [TestClass] + public class UnitTestConstructorVisibilityTests + { + [TestMethod] + public void GetConstructor_PublicParameterless_IsFound() + { + ConstructorInfo constructor = typeof(ClassWithPublicConstructor).GetConstructor(new Type[0]); + + Assert.IsNotNull(constructor); + Assert.IsTrue(constructor.IsPublic); + } + + [TestMethod] + public void GetConstructor_NonPublicParameterless_IsNull() + { + Assert.IsNull(typeof(ClassWithPrivateConstructors).GetConstructor(new Type[0])); + Assert.IsNull(typeof(ClassWithProtectedConstructor).GetConstructor(new Type[0])); + Assert.IsNull(typeof(ClassWithInternalConstructor).GetConstructor(new Type[0])); + } + + [TestMethod] + public void GetConstructor_NonPublicWithParameters_IsNull() + { + Assert.IsNull(typeof(ClassWithPrivateConstructors).GetConstructor(new Type[] { typeof(int) })); + Assert.IsNull(typeof(ClassWithMixedConstructors).GetConstructor(new Type[] { typeof(int) })); + Assert.IsNull(typeof(ClassWithMixedConstructors).GetConstructor(new Type[] { typeof(string) })); + Assert.IsNull(typeof(ClassWithMixedConstructors).GetConstructor(new Type[] { typeof(long) })); + Assert.IsNotNull(typeof(ClassWithMixedConstructors).GetConstructor(new Type[] { typeof(char) })); + } + + [TestMethod] + public void GetConstructors_ReturnsOnlyPublicOnes() + { + Assert.AreEqual(2, typeof(ClassWithMixedConstructors).GetConstructors().Length); + Assert.AreEqual(0, typeof(ClassWithPrivateConstructors).GetConstructors().Length); + } + + [TestMethod] + public void Invoke_PublicConstructor_CreatesTheObject() + { + object created = typeof(ClassWithPublicConstructor).GetConstructor(new Type[0]).Invoke(null); + + Assert.IsInstanceOfType(created, typeof(ClassWithPublicConstructor)); + Assert.AreEqual(1, ((ClassWithPublicConstructor)created).X); + + Assert.IsInstanceOfType(typeof(GenericClassWithPublicConstructor).GetConstructor(new Type[0]).Invoke(null), typeof(GenericClassWithPublicConstructor)); + } + + [TestMethod] + public void Invoke_AbstractClassConstructor_ThrowsMemberAccessException() + { + ConstructorInfo constructor = typeof(AbstractClassWithPublicConstructor).GetConstructor(new Type[0]); + + Assert.IsNotNull(constructor); + Assert.ThrowsException(typeof(MemberAccessException), () => constructor.Invoke(null)); + + ConstructorInfo closed = typeof(AbstractGenericClass).GetConstructor(new Type[0]); + + Assert.ThrowsException(typeof(MemberAccessException), () => closed.Invoke(null)); + } + + [TestMethod] + public void Invoke_OpenGenericConstructor_ThrowsMemberAccessException() + { + ConstructorInfo open = typeof(GenericClassWithPublicConstructor<>).GetConstructor(new Type[0]); + + Assert.IsNotNull(open); + Assert.ThrowsException(typeof(MemberAccessException), () => open.Invoke(null)); + + ConstructorInfo openAbstract = typeof(AbstractGenericClass<>).GetConstructor(new Type[0]); + + Assert.ThrowsException(typeof(MemberAccessException), () => openAbstract.Invoke(null)); + } + + [TestMethod] + public void InterfaceType_HasNoConstructors() + { + Assert.IsNull(typeof(IMarkerInterface).GetConstructor(new Type[0])); + Assert.AreEqual(0, typeof(IMarkerInterface).GetConstructors().Length); + } + + [TestMethod] + public void Activator_AbstractOrNonPublic_ThrowsMissingMethodException() + { + Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance()); + Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance()); + Assert.ThrowsException(typeof(MissingMethodException), () => Activator.CreateInstance()); + Assert.IsNotNull(Activator.CreateInstance()); + } + } +} diff --git a/Tests/NFUnitTestSystemLibReflection3/UnitTestGenericLimitTests.cs b/Tests/NFUnitTestSystemLibReflection3/UnitTestGenericLimitTests.cs new file mode 100644 index 00000000..db97c80f --- /dev/null +++ b/Tests/NFUnitTestSystemLibReflection3/UnitTestGenericLimitTests.cs @@ -0,0 +1,126 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Reflection; +using nanoFramework.TestFramework; + +namespace NFUnitTestSystemLibReflection3 +{ + public class NestableBox + { + public T V; + } + + public class GenericWith15Arguments + { + } + + public class GenericWith16Arguments + { + } + + public static class GenericCastHelper + { + public static bool Is(object o) + { + return o is T; + } + + public static object Cast(object o) + { + return (T)o; + } + } + + [TestClass] + public class UnitTestGenericLimitTests + { + [TestMethod] + public void Cast_WithinTheLimits_ComparesTheArguments() + { + object many = new GenericWith15Arguments(); + object box = new NestableBox>>>(); + + Assert.IsTrue(many is GenericWith15Arguments); + Assert.IsFalse(many is GenericWith15Arguments); + Assert.ThrowsException( + typeof(InvalidCastException), + () => { var other = (GenericWith15Arguments)many; }); + + Assert.IsTrue(box is NestableBox>>>); + Assert.IsFalse(box is NestableBox>>>); + Assert.IsTrue(GenericCastHelper.Is>>>>(box)); + Assert.IsFalse(GenericCastHelper.Is>>>>(box)); + } + + [TestMethod] + public void Cast_ToTypeWithTooManyNodes_IsNotSupported() + { + object many = new GenericWith16Arguments(); + + Assert.ThrowsException( + typeof(NotSupportedException), + () => { bool b = many is GenericWith16Arguments; }); + Assert.ThrowsException( + typeof(NotSupportedException), + () => { var other = (GenericWith16Arguments)many; }); + Assert.ThrowsException( + typeof(NotSupportedException), + () => GenericCastHelper.Is>(many)); + Assert.ThrowsException( + typeof(NotSupportedException), + () => GenericCastHelper.Cast>(many)); + } + + [TestMethod] + public void Cast_ToTypeNestedTooDeep_IsNotSupported() + { + object box = new NestableBox>>>(); + + Assert.ThrowsException(typeof(NotSupportedException), () => GenericCastHelper.Is>>>>>(box)); + Assert.ThrowsException(typeof(NotSupportedException), () => GenericCastHelper.Cast>>>>>(box)); + } + + [TestMethod] + public void Cast_ArrayToArrayOfTypeNestedTooDeep_IsNotSupported() + { + object boxes = new NestableBox>>>[1]; + + Assert.ThrowsException( + typeof(NotSupportedException), + () => { bool b = boxes is NestableBox>>>>[]; }); + Assert.ThrowsException( + typeof(NotSupportedException), + () => GenericCastHelper.Is>>>>[]>(boxes)); + Assert.ThrowsException( + typeof(NotSupportedException), + () => GenericCastHelper.Cast>>>>[]>(boxes)); + Assert.IsFalse(boxes is NestableBox>>>[]); + } + + [TestMethod] + public void MakeGenericType_OverTheLimits_IsNotSupported() + { + Type[] sixteen = new Type[16]; + + for (int i = 0; i < sixteen.Length; i++) + { + sixteen[i] = typeof(int); + } + + Assert.ThrowsException(typeof(NotSupportedException), () => typeof(GenericWith16Arguments<,,,,,,,,,,,,,,,>).MakeGenericType(sixteen)); + + Type deep = typeof(int); + + for (int i = 0; i < 4; i++) + { + deep = typeof(NestableBox<>).MakeGenericType(deep); + } + + Type tooDeep = deep; + + Assert.ThrowsException(typeof(NotSupportedException), () => typeof(NestableBox<>).MakeGenericType(tooDeep)); + } + } +}