Fix emitted IgnoreAccessChecksToAttribute.AssemblyName property (#45029)
authorEric StJohn <ericstj@microsoft.com>
Wed, 9 Dec 2020 16:27:38 +0000 (08:27 -0800)
committerGitHub <noreply@github.com>
Wed, 9 Dec 2020 16:27:38 +0000 (16:27 +0000)
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()
}
```

src/libraries/Common/src/System/Reflection/Emit/IgnoreAccessChecksToAttributeBuilder.cs
src/libraries/System.Reflection.DispatchProxy/tests/DispatchProxyTests.cs

index 3b6d2b5..dac3584 100644 (file)
@@ -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;
index ac7be18..d4a421b 100644 (file)
@@ -47,6 +47,21 @@ namespace DispatchProxyTests
         {
             TestType_InternalInterfaceService proxy = DispatchProxy.Create<TestType_PublicInterfaceService_Implements_Internal, TestDispatchProxy>();
             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]