diff --git a/MetadataProcessor.Shared/nanoAssemblyBuilder.cs b/MetadataProcessor.Shared/nanoAssemblyBuilder.cs index 7a2f5e27..1396f5fc 100644 --- a/MetadataProcessor.Shared/nanoAssemblyBuilder.cs +++ b/MetadataProcessor.Shared/nanoAssemblyBuilder.cs @@ -713,7 +713,16 @@ private HashSet BuildDependencyList(MetadataToken token) { if (v.VariableType.DeclaringType != null) { - set.Add(v.VariableType.DeclaringType.MetadataToken); + var resolvedType = v.VariableType.Resolve(); + + if (resolvedType != null && resolvedType.IsEnum) + { + set.Add(v.VariableType.MetadataToken); + } + else + { + set.Add(v.VariableType.DeclaringType.MetadataToken); + } } else if (v.VariableType.MetadataType == MetadataType.Class) { diff --git a/MetadataProcessor.Tests/Core/Utility/DumperTests.cs b/MetadataProcessor.Tests/Core/Utility/DumperTests.cs index d4ef58a0..5b0ad9b0 100644 --- a/MetadataProcessor.Tests/Core/Utility/DumperTests.cs +++ b/MetadataProcessor.Tests/Core/Utility/DumperTests.cs @@ -41,9 +41,9 @@ public void DumpAssemblyTest() Assert.IsTrue(dumpFileContent.Contains("TypeRefProps [01000001]: Scope: 23000001 'System.Diagnostics.DebuggableAttribute'")); Assert.IsTrue(dumpFileContent.Contains(": Scope: 23000002 'TestNFClassLibrary.ClassOnAnotherAssembly'")); - Assert.IsTrue(dumpFileContent.Contains(": Flags: 00001001 Extends: 0100000d Enclosed: 02000000 'TestNFApp.DummyCustomAttribute1'")); + Assert.IsTrue(dumpFileContent.Contains(": Flags: 00001001 Extends: 0100000f Enclosed: 02000000 'TestNFApp.DummyCustomAttribute1'")); - Assert.IsTrue(dumpFileContent.Contains(": Flags: 00001001 Extends: 0100000d Enclosed: 02000000 'TestNFApp.DummyCustomAttribute2'")); + Assert.IsTrue(dumpFileContent.Contains(": Flags: 00001001 Extends: 0100000f Enclosed: 02000000 'TestNFApp.DummyCustomAttribute2'")); Assert.IsTrue(dumpFileContent.Contains(": Flags: 00001061 Extends: 01000000 Enclosed: 02000000 'TestNFApp.IOneClassOverAll'")); Assert.IsTrue(dumpFileContent.Contains(": Flags: 000007c6 Impl: 00000000 RVA: 00000000 'get_DummyProperty' [I4( )]")); diff --git a/MetadataProcessor.Tests/TestNFApp/Program.cs b/MetadataProcessor.Tests/TestNFApp/Program.cs index 9255f545..443706de 100644 --- a/MetadataProcessor.Tests/TestNFApp/Program.cs +++ b/MetadataProcessor.Tests/TestNFApp/Program.cs @@ -5,6 +5,7 @@ using System; using System.Diagnostics; +using System.IO; using System.Reflection; using TestNFClassLibrary; @@ -36,6 +37,26 @@ public static void Main() // Reflection Tests ReflectionTests(); + //////////////////////////////////////////////// + // Test enum in another assembly, same namespace + var enumTest = new TestEnumInAnotherAssembly(); + enumTest.CallTestEnumInAnotherAssembly(); + + ///////////////////////////////////// + // reference enum in another assembly + var x = (IAmAClassWithAnEnum.EnumA)1; + + var messageType = (IAmAClassWithAnEnum.EnumA)0; + switch (messageType) + { + case IAmAClassWithAnEnum.EnumA.Test: + Console.WriteLine("all good"); + break; + + default: + break; + } + Debug.WriteLine("Exiting TestNFApp"); } diff --git a/MetadataProcessor.Tests/TestNFApp/TestEnumInAnotherAssembly.cs b/MetadataProcessor.Tests/TestNFApp/TestEnumInAnotherAssembly.cs new file mode 100644 index 00000000..1f1fc49e --- /dev/null +++ b/MetadataProcessor.Tests/TestNFApp/TestEnumInAnotherAssembly.cs @@ -0,0 +1,20 @@ +// +// Copyright (c) .NET Foundation and Contributors +// See LICENSE file in the project root for full license information. +// + +namespace System.IO +{ + public class TestEnumInAnotherAssembly + { + public void CallTestEnumInAnotherAssembly() + { + // This test checks if MDP can minimize the assembly using an enum that is defined in another assembly + // and the class calling it is in a different assembly BUT in the same namespace. + var ioException = new IOException( + string.Empty, + (int)IOException.IOExceptionErrorCode.DirectoryNotFound); + } + } + +} diff --git a/MetadataProcessor.Tests/TestNFApp/TestNFApp.nfproj b/MetadataProcessor.Tests/TestNFApp/TestNFApp.nfproj index a358f092..927995f6 100644 --- a/MetadataProcessor.Tests/TestNFApp/TestNFApp.nfproj +++ b/MetadataProcessor.Tests/TestNFApp/TestNFApp.nfproj @@ -26,6 +26,7 @@ + diff --git a/MetadataProcessor.Tests/TestNFClassLibrary/TestNFClassLibrary/IAmAClassWithAnEnum.cs b/MetadataProcessor.Tests/TestNFClassLibrary/TestNFClassLibrary/IAmAClassWithAnEnum.cs new file mode 100644 index 00000000..4a45c107 --- /dev/null +++ b/MetadataProcessor.Tests/TestNFClassLibrary/TestNFClassLibrary/IAmAClassWithAnEnum.cs @@ -0,0 +1,15 @@ +// +// Copyright (c) .NET Foundation and Contributors +// See LICENSE file in the project root for full license information. +// + +namespace TestNFClassLibrary +{ + public class IAmAClassWithAnEnum + { + public enum EnumA + { + Test = 1 + } + } +} diff --git a/MetadataProcessor.Tests/TestNFClassLibrary/TestNFClassLibrary/TestNFClassLibrary.nfproj b/MetadataProcessor.Tests/TestNFClassLibrary/TestNFClassLibrary/TestNFClassLibrary.nfproj index 4067964d..9f9f51f3 100644 --- a/MetadataProcessor.Tests/TestNFClassLibrary/TestNFClassLibrary/TestNFClassLibrary.nfproj +++ b/MetadataProcessor.Tests/TestNFClassLibrary/TestNFClassLibrary/TestNFClassLibrary.nfproj @@ -18,6 +18,7 @@ +