From f8319a8d15c8d184f02097f4fedd09b608207ede Mon Sep 17 00:00:00 2001 From: Romfos Date: Wed, 15 May 2024 11:45:36 +0200 Subject: [PATCH] Hotfix Issue500_SpecialMethodsWithoutAttribute --- ...Issue500_SpecialMethodsWithoutAttribute.cs | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs b/tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs index 089e5f71..42ae5008 100644 --- a/tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs +++ b/tests/NSubstitute.Acceptance.Specs/FieldReports/Issue500_SpecialMethodsWithoutAttribute.cs @@ -1,6 +1,6 @@ +using NUnit.Framework; using System.Reflection; using System.Reflection.Emit; -using NUnit.Framework; namespace NSubstitute.Acceptance.Specs.FieldReports; @@ -18,7 +18,7 @@ static Issue500_SpecialMethodsWithoutAttribute() [Test] public void ShouldCorrectlyConfigureProperty() { - var substitute = Substitute.For([TypeWithMissingSpecialNameMethodAttributes], []); + var substitute = Substitute.For(new[] { TypeWithMissingSpecialNameMethodAttributes }, new object[0]); var fixture = new GeneratedTypeFixture(substitute); fixture.MyProperty = "42"; @@ -30,7 +30,7 @@ public void ShouldCorrectlyConfigureProperty() [Test] public void ShouldCorrectlyConfigureEvent() { - object substitute = Substitute.For([TypeWithMissingSpecialNameMethodAttributes], []); + object substitute = Substitute.For(new[] { TypeWithMissingSpecialNameMethodAttributes }, new object[0]); var fixture = new GeneratedTypeFixture(substitute); bool wasCalled = false; @@ -52,19 +52,20 @@ private static Type GenerateTypeWithMissingSpecialNameAttributes() var typeBuilder = module.DefineType("TypeWithMissingSpecialAttributes", TypeAttributes.Public | TypeAttributes.Abstract); + typeBuilder.DefineDefaultConstructor(MethodAttributes.Public); var evBuilder = typeBuilder.DefineEvent(EventName, EventAttributes.None, typeof(EventHandler)); var evAdder = typeBuilder.DefineMethod( $"add_{EventName}", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.Abstract | MethodAttributes.HideBySig | MethodAttributes.NewSlot /* | MethodAttributes.SpecialName */, typeof(void), - [typeof(EventHandler)]); + new[] { typeof(EventHandler) }); var evRemover = typeBuilder.DefineMethod( $"remove_{EventName}", MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.Abstract | MethodAttributes.HideBySig | MethodAttributes.NewSlot /* | MethodAttributes.SpecialName */, typeof(void), - [typeof(EventHandler)]); + new[] { typeof(EventHandler) }); evBuilder.SetAddOnMethod(evAdder); evBuilder.SetRemoveOnMethod(evRemover); @@ -81,25 +82,32 @@ private static Type GenerateTypeWithMissingSpecialNameAttributes() MethodAttributes.Public | MethodAttributes.Virtual | MethodAttributes.Abstract | MethodAttributes.HideBySig | MethodAttributes.NewSlot /* | MethodAttributes.SpecialName */, typeof(void), - [typeof(object)]); + new[] { typeof(object) }); propBuilder.SetGetMethod(propGetter); propBuilder.SetSetMethod(propSetter); return typeBuilder.CreateTypeInfo().AsType(); } - private class GeneratedTypeFixture(object substitute) + private class GeneratedTypeFixture { + private readonly object _substitute; + + public GeneratedTypeFixture(object substitute) + { + _substitute = substitute; + } + public object MyProperty { - get => substitute.GetType().GetProperty(PropertyName).GetValue(substitute); - set => substitute.GetType().GetProperty(PropertyName).SetValue(substitute, value); + get => _substitute.GetType().GetProperty(PropertyName).GetValue(_substitute); + set => _substitute.GetType().GetProperty(PropertyName).SetValue(_substitute, value); } public event EventHandler MyEvent { - add => substitute.GetType().GetEvent(EventName).AddEventHandler(substitute, value); - remove => substitute.GetType().GetEvent(EventName).RemoveEventHandler(substitute, value); + add => _substitute.GetType().GetEvent(EventName).AddEventHandler(_substitute, value); + remove => _substitute.GetType().GetEvent(EventName).RemoveEventHandler(_substitute, value); } } } \ No newline at end of file