From fa8c8098ef1507a05df555c20e07731c2ae9d638 Mon Sep 17 00:00:00 2001 From: Eric StJohn Date: Wed, 9 Dec 2020 08:27:38 -0800 Subject: [PATCH] Fix emitted IgnoreAccessChecksToAttribute.AssemblyName property (#45029) Previously this would emit an empty property. ``` .property instance string AssemblyName() { } ``` Now it correctly emits a property with getter. ``` .property instance string AssemblyName() { .get instance string System.Runtime.CompilerServices.IgnoresAccessChecksToAttribute::get_AssemblyName() } ``` --- .../Emit/IgnoreAccessChecksToAttributeBuilder.cs | 3 ++- .../tests/DispatchProxyTests.cs | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/libraries/Common/src/System/Reflection/Emit/IgnoreAccessChecksToAttributeBuilder.cs b/src/libraries/Common/src/System/Reflection/Emit/IgnoreAccessChecksToAttributeBuilder.cs index 3b6d2b5..dac3584 100644 --- a/src/libraries/Common/src/System/Reflection/Emit/IgnoreAccessChecksToAttributeBuilder.cs +++ b/src/libraries/Common/src/System/Reflection/Emit/IgnoreAccessChecksToAttributeBuilder.cs @@ -47,7 +47,7 @@ namespace System.Reflection.Emit // Define property as: // public string AssemblyName {get { return this.assemblyName; } } - _ = attributeTypeBuilder.DefineProperty( + PropertyBuilder propertyBuilder = attributeTypeBuilder.DefineProperty( "AssemblyName", PropertyAttributes.None, CallingConventions.HasThis, @@ -60,6 +60,7 @@ namespace System.Reflection.Emit CallingConventions.HasThis, returnType: typeof(string), parameterTypes: null); + propertyBuilder.SetGetMethod(getterMethodBuilder); // Generate body: // return this.assemblyName; diff --git a/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs b/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs index ac7be18..d4a421b 100644 --- a/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs +++ b/src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs @@ -47,6 +47,21 @@ namespace DispatchProxyTests { TestType_InternalInterfaceService proxy = DispatchProxy.Create(); Assert.NotNull(proxy); + + // ensure we emit a valid attribute definition + Type iactAttributeType = proxy.GetType().Assembly.GetType("System.Runtime.CompilerServices.IgnoresAccessChecksToAttribute"); + Assert.NotNull(iactAttributeType); + ConstructorInfo constructor = iactAttributeType.GetConstructor(new[] { typeof(string) }); + Assert.NotNull(constructor); + PropertyInfo propertyInfo = iactAttributeType.GetProperty("AssemblyName"); + Assert.NotNull(propertyInfo); + Assert.NotNull(propertyInfo.GetMethod); + + string name = "anAssemblyName"; + object attributeInstance = constructor.Invoke(new object[] { name }); + Assert.NotNull(attributeInstance); + object actualName = propertyInfo.GetMethod.Invoke(attributeInstance, null); + Assert.Equal(name, actualName); } [Fact] -- 2.7.4