Skip to content

Commit c4845d6

Browse files
committed
Add edge case with referencing enums in another assembly
- Dependency crawler now properly finds usage of enums from other assemblies. - Add edge case to Unit Test test app. - Update dumper unit test because of new types added.
1 parent d88efb0 commit c4845d6

File tree

7 files changed

+70
-3
lines changed

7 files changed

+70
-3
lines changed

MetadataProcessor.Shared/nanoAssemblyBuilder.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,16 @@ private HashSet<MetadataToken> BuildDependencyList(MetadataToken token)
713713
{
714714
if (v.VariableType.DeclaringType != null)
715715
{
716-
set.Add(v.VariableType.DeclaringType.MetadataToken);
716+
var resolvedType = v.VariableType.Resolve();
717+
718+
if (resolvedType != null && resolvedType.IsEnum)
719+
{
720+
set.Add(v.VariableType.MetadataToken);
721+
}
722+
else
723+
{
724+
set.Add(v.VariableType.DeclaringType.MetadataToken);
725+
}
717726
}
718727
else if (v.VariableType.MetadataType == MetadataType.Class)
719728
{

MetadataProcessor.Tests/Core/Utility/DumperTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ public void DumpAssemblyTest()
4141
Assert.IsTrue(dumpFileContent.Contains("TypeRefProps [01000001]: Scope: 23000001 'System.Diagnostics.DebuggableAttribute'"));
4242
Assert.IsTrue(dumpFileContent.Contains(": Scope: 23000002 'TestNFClassLibrary.ClassOnAnotherAssembly'"));
4343

44-
Assert.IsTrue(dumpFileContent.Contains(": Flags: 00001001 Extends: 0100000d Enclosed: 02000000 'TestNFApp.DummyCustomAttribute1'"));
44+
Assert.IsTrue(dumpFileContent.Contains(": Flags: 00001001 Extends: 0100000f Enclosed: 02000000 'TestNFApp.DummyCustomAttribute1'"));
4545

46-
Assert.IsTrue(dumpFileContent.Contains(": Flags: 00001001 Extends: 0100000d Enclosed: 02000000 'TestNFApp.DummyCustomAttribute2'"));
46+
Assert.IsTrue(dumpFileContent.Contains(": Flags: 00001001 Extends: 0100000f Enclosed: 02000000 'TestNFApp.DummyCustomAttribute2'"));
4747

4848
Assert.IsTrue(dumpFileContent.Contains(": Flags: 00001061 Extends: 01000000 Enclosed: 02000000 'TestNFApp.IOneClassOverAll'"));
4949
Assert.IsTrue(dumpFileContent.Contains(": Flags: 000007c6 Impl: 00000000 RVA: 00000000 'get_DummyProperty' [I4( )]"));

MetadataProcessor.Tests/TestNFApp/Program.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using System;
77
using System.Diagnostics;
8+
using System.IO;
89
using System.Reflection;
910
using TestNFClassLibrary;
1011

@@ -36,6 +37,26 @@ public static void Main()
3637
// Reflection Tests
3738
ReflectionTests();
3839

40+
////////////////////////////////////////////////
41+
// Test enum in another assembly, same namespace
42+
var enumTest = new TestEnumInAnotherAssembly();
43+
enumTest.CallTestEnumInAnotherAssembly();
44+
45+
/////////////////////////////////////
46+
// reference enum in another assembly
47+
var x = (IAmAClassWithAnEnum.EnumA)1;
48+
49+
var messageType = (IAmAClassWithAnEnum.EnumA)0;
50+
switch (messageType)
51+
{
52+
case IAmAClassWithAnEnum.EnumA.Test:
53+
Console.WriteLine("all good");
54+
break;
55+
56+
default:
57+
break;
58+
}
59+
3960
Debug.WriteLine("Exiting TestNFApp");
4061
}
4162

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace System.IO
7+
{
8+
public class TestEnumInAnotherAssembly
9+
{
10+
public void CallTestEnumInAnotherAssembly()
11+
{
12+
// This test checks if MDP can minimize the assembly using an enum that is defined in another assembly
13+
// and the class calling it is in a different assembly BUT in the same namespace.
14+
var ioException = new IOException(
15+
string.Empty,
16+
(int)IOException.IOExceptionErrorCode.DirectoryNotFound);
17+
}
18+
}
19+
20+
}

MetadataProcessor.Tests/TestNFApp/TestNFApp.nfproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
<Compile Include="IOneClassOverAll.cs" />
2727
<Compile Include="ComplexAttribute.cs" />
2828
<Compile Include="MaxAttribute.cs" />
29+
<Compile Include="TestEnumInAnotherAssembly.cs" />
2930
<Compile Include="MyAttribute.cs" />
3031
<Compile Include="MyClass1.cs" />
3132
<Compile Include="OneClassOverAll.cs" />
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace TestNFClassLibrary
7+
{
8+
public class IAmAClassWithAnEnum
9+
{
10+
public enum EnumA
11+
{
12+
Test = 1
13+
}
14+
}
15+
}

MetadataProcessor.Tests/TestNFClassLibrary/TestNFClassLibrary/TestNFClassLibrary.nfproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
</PropertyGroup>
1919
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
2020
<ItemGroup>
21+
<Compile Include="IAmAClassWithAnEnum.cs" />
2122
<Compile Include="ClassOnAnotherAssembly.cs" />
2223
<Compile Include="Properties\AssemblyInfo.cs" />
2324
</ItemGroup>

0 commit comments

Comments
 (0)