Closed krafs closed 5 months ago
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:
public class Parent
{
public virtual void Hello() { Console.WriteLine("PARENT"); }
}
public class Child : Parent
{
public override void Hello() { 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
?
The purpose is the same. The technical limitations are not the same. SymbolExtensions basically wrap a super simple concept and there is no way to improve that: < https://github.com/pardeike/Harmony/blob/669729f618e90ecf7fe943f7ed70f8150216dc5a/Harmony/Tools/SymbolExtensions.cs#L47>
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?