Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorg: Incorrect behaviour of MemberReference.Is(MemberInfo) extensions with module level on mono #173

Open
kohanis opened this issue Mar 14, 2024 · 2 comments
Labels

Comments

@kohanis
Copy link

kohanis commented Mar 14, 2024

Description

Because <Module> is an actual type on mono MonoMod.Utils.Extensions.Is(this MemberReference? mref, MemberInfo? minfo) is failing

var mrefDecl = mref.DeclaringType;
if (mrefDecl?.FullName == "<Module>")
mrefDecl = null;

if (minfo.DeclaringType != null)
{
if (mrefDecl == null)
return false;

Example

using var assemblyDefinition = AssemblyDefinition.CreateAssembly(new AssemblyNameDefinition("TestAssembly", new Version(1, 0)), "MainModule.dll", ModuleKind.Dll);

var moduleType = assemblyDefinition.MainModule.Types.First(m => m.Name == "<Module>");

var testMethod = new MethodDefinition("ModuleTest", MethodAttributes.Static | MethodAttributes.Public, assemblyDefinition.MainModule.TypeSystem.Void);
var il = testMethod.Body.GetILProcessor();
il.Emit(OpCodes.Ret);
moduleType.Methods.Add(testMethod);

using var ms = new MemoryStream();
assemblyDefinition.Write(ms);
var assembly = System.Reflection.Assembly.Load(ms.ToArray());

var reflectMethod = assembly.ManifestModule.GetMethod("ModuleTest");

Console.WriteLine(testMethod.Is(reflectMethod));
Console.WriteLine(reflectMethod.ReflectedType?.FullName ?? "null");
False
<Module>

on mono

@kohanis kohanis added the bug label Mar 14, 2024
@nike4613
Copy link
Contributor

If you don't mind me asking, how are you running into this? Generally, methods on <Module> are extremely rare. The only cases I've found that come out of the C# compiler are a handful of synthesized methods (like an string hash function for large switch statements) or the module initializer itself.

@kohanis
Copy link
Author

kohanis commented Mar 14, 2024

Test in HarmonyX specifically for this, which stopped working)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants