Skip to content

Commit

Permalink
Override IsEnum on TypeDef and TypeRef
Browse files Browse the repository at this point in the history
Override Type.IsEnum to correctly handle different corelibs

Issue: #121
  • Loading branch information
MSDN-WhiteKnight committed Apr 1, 2023
1 parent 01e4b32 commit 1f706fd
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
13 changes: 13 additions & 0 deletions CilTools.Metadata/TypeDef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,19 @@ public override bool IsGenericType

public override bool IsGenericTypeDefinition => this.IsGenericType;

public override bool IsEnum
{
get
{
// Overridden to return correct result when inspected type's corelib does not match current runtime corelib
Type bt = this.BaseType;

if (bt == null) return false;

return Utils.StrEquals(bt.FullName, "System.Enum") && Utils.IsCoreAssembly(bt.Assembly);
}
}

public override Type[] GetGenericArguments()
{
GenericParameterHandleCollection hcoll = type.GetGenericParameters();
Expand Down
13 changes: 13 additions & 0 deletions CilTools.Metadata/TypeRef.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,19 @@ public override Type DeclaringType
}
}

public override bool IsEnum
{
get
{
// Overridden to return correct result when inspected type's corelib does not match current runtime corelib
Type bt = this.BaseType;

if (bt == null) return false;

return Utils.StrEquals(bt.FullName, "System.Enum") && Utils.IsCoreAssembly(bt.Assembly);
}
}

public override bool IsAssignableFrom(Type c)
{
if (c.IsInterface || this.IsInterface)
Expand Down
18 changes: 18 additions & 0 deletions tests/CilTools.Metadata.Tests/TypeDefTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -970,5 +970,23 @@ public void Test_IsValueType_Class(Type t)
{
Assert.IsFalse(t.IsValueType);
}

[TestMethod]
[TypeTestData(typeof(BytecodeProviders), BytecodeProviders.Metadata)]
public void Test_IsEnum(Type t)
{
Assert.IsTrue(t.IsEnum);
Assert.IsTrue(t.IsValueType);
Assert.IsFalse(t.IsClass);
Assert.IsFalse(t.IsInterface);
}

[TestMethod]
[TypeTestData(typeof(Attribute), BytecodeProviders.Metadata)]
public void Test_IsEnum_Negative(Type t)
{
Assert.IsFalse(t.IsEnum);
Assert.IsTrue(t.IsClass);
}
}
}
10 changes: 10 additions & 0 deletions tests/CilTools.Metadata.Tests/TypeRefTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ public void Test_IsValueType_Interface()
Assert.IsFalse(t.IsValueType);
Assert.IsTrue(t.IsInterface);
}

[TestMethod]
public void Test_IsEnum()
{
Type t = TypeRefTests_Data.GetTypeRef(typeof(AttributeTargets).FullName);
Assert.IsTrue(t.IsEnum);

t = TypeRefTests_Data.GetTypeRef(typeof(Console).FullName);
Assert.IsFalse(t.IsEnum);
}
}

public class TypeRefTests_Data
Expand Down

0 comments on commit 1f706fd

Please sign in to comment.