Skip to content

Commit

Permalink
Issue Lokad#172 - Never add method implementation if the declaration …
Browse files Browse the repository at this point in the history
…handle equals to body handle (default interface method case).
  • Loading branch information
OlegRa committed May 1, 2023
1 parent 2003f50 commit d8389ad
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
18 changes: 15 additions & 3 deletions src/AssemblyGenerator.Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,16 +209,28 @@ HashSet<Type> interfaces
var targetMethod = interfaceMapping.TargetMethods[i];
var ifcMethod = interfaceMapping.InterfaceMethods[i];

if (targetMethod == null || ifcMethod == null)
{
continue;
}

var bodyHandle =_metadata.GetMethodHandle(targetMethod);
var declarationHandle = _metadata.GetMethodHandle(ifcMethod);

if (bodyHandle == declarationHandle)
{
continue;
}

// Declare a method override either when the interface implementation or
// the interface method implementation is declared by the type.
if (targetMethod != null && (implementedByType || targetMethod.DeclaringType == type))
if (implementedByType || targetMethod.DeclaringType == type)
{
// Mark the target method as implementing the interface method.
// (This is the equivalent of .Override in msil)
_metadata.Builder.AddMethodImplementation(
(TypeDefinitionHandle)_metadata.GetTypeHandle(targetMethod.DeclaringType),
_metadata.GetMethodHandle(targetMethod),
_metadata.GetMethodHandle(ifcMethod));
bodyHandle, declarationHandle);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Lokad.ILPack.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<OutputType>Library</OutputType>
<LangVersion>8.0</LangVersion>

Expand All @@ -13,7 +13,7 @@
<Copyright>Copyright © Lokad 2020 - 2022</Copyright>

<PackageId>Lokad.ILPack</PackageId>

<AssemblyVersion>0.2.0.0</AssemblyVersion>
<FileVersion>0.2.0.0</FileVersion>
<PackageVersion>0.2.0.0</PackageVersion>
Expand Down
4 changes: 4 additions & 0 deletions test/TestSubject/IMyItf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,9 @@ public interface IMyItf
int InterfaceMethod2();

TResult InterfaceMethod3<TResult>(Func<TResult> f);

#if NETCOREAPP3_0_OR_GREATER
void DefaultInterfaceMethod() { }
#endif
}
}
2 changes: 1 addition & 1 deletion test/TestSubject/TestSubject.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\Lokad.ILPack.snk</AssemblyOriginatorKeyFile>
Expand Down

0 comments on commit d8389ad

Please sign in to comment.