You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
Getting a MethodInfo for an overriden method using SymbolExtensions resolves the base class method, not the child class method.
To Reproduce
Open a terminal in a new empty folder.
dotnet new console --framework net8.0
dotnet add package Lib.Harmony --version 2.3.1.1
Create file called Program.cs and paste the code below
Save the file
dotnet run
Expected behavior
I expect the MethodInfo retrieved for the overriden method in Child class using SymbolExtensions to be the same MethodInfo I get when retrieving it using regular reflection.
Code
using HarmonyLib;using System.Reflection;MethodInfoparentHello=typeof(Parent).GetMethod("Hello");MethodInfochildHello=typeof(Child).GetMethod("Hello");
Console.WriteLine(parentHello==childHello);// Expected: False// Actual: FalseMethodInfosymbolParentHello= SymbolExtensions.GetMethodInfo<Parent>(x => x.Hello());MethodInfosymbolChildHello= SymbolExtensions.GetMethodInfo<Child>(x => x.Hello());// Check if MethodInfo for Child.Hello resolved via Reflection is same the one from SymbolExtensions
Console.WriteLine(childHello==symbolChildHello);// Expected: True// Actual: False// Check if MethodInfo for Parent.Hello and Child.Hello resolved via SymbolExtensions are the same
Console.WriteLine(symbolParentHello==symbolChildHello);// Expected: False// Actual: TruepublicclassParent{publicvirtualvoidHello(){}}publicclassChild:Parent{publicoverridevoidHello(){}}
Runtime environment:
OS: Windows 11, 64bit
.NET version: .NET 8.0/.NET 472
Harmony version: 2.3.1.1
I couldn't find any tests on SymbolExtensions, so not sure if this is an expected limitation on the API?
The text was updated successfully, but these errors were encountered:
The reason is that reflections do not work well with inheritance. Its the same reason that does not allow you to call a base method on a child instance if you don't let the compiler do it. That includes casting, ordinary reflection and delegates. It's the reason Harmony has a reverse patch. I extended your code to demonstrate this effect:
publicclassParent{publicvirtualvoidHello(){ Console.WriteLine("PARENT");}}publicclassChild:Parent{publicoverridevoidHello(){ Console.WriteLine("CHILD");}}
parentHello.Invoke(new Parent(),null);
parentHello.Invoke(new Child(),null);// should log PARENT but logsCHILD
childHello.Invoke(new Child(),null);
SymbolExtension uses reflection and thus cannot choose which level of overwrite it should get.
Maybe I've misunderstood the use of SymbolExtensions then. Is SymbolExtensions not for getting a MethodInfo on a given type? It doesn't need to determine the level of the overwrite to return, I am telling it to return the one from Child.
This gets me what I expect. MethodInfo hello1 = typeof(Child).GetMethod("Hello");
And this call has all the same amount of information, so I feel like it should be able to give the same result, no? MethodInfo hello2 = SymbolExtensions.GetMethodInfo<Child>(x => x.Hello());
Or is this not the purpose of SymbolExtensions? It's not an alternative to Reflection for getting a MethodInfo?
Describe the bug
Getting a
MethodInfo
for an overriden method usingSymbolExtensions
resolves the base class method, not the child class method.To Reproduce
dotnet new console --framework net8.0
dotnet add package Lib.Harmony --version 2.3.1.1
Program.cs
and paste the code belowdotnet run
Expected behavior
I expect the
MethodInfo
retrieved for the overriden method inChild
class usingSymbolExtensions
to be the sameMethodInfo
I get when retrieving it using regular reflection.Code
Runtime environment:
I couldn't find any tests on
SymbolExtensions
, so not sure if this is an expected limitation on the API?The text was updated successfully, but these errors were encountered: