NullReferenceException when trying to resolve ModuleReference members
See original GitHub issueThe testcode i used is copied from https://docs.microsoft.com/en-us/dotnet/framework/app-domains/build-multifile-assembly
Scenario:
I compiled the tutorial from the link above.
This yields the following files: myAssembly.exe
Client.netmodule
and Stringer.netmodule
I’m trying to resolve as many Reference members into their matching Definitions (e.g. TypeReference --> TypeDefinition)
Error Description:
i’m running the following code:
var appA = AssemblyDefinition.ReadAssembly("myAssembly.exe");
// Get all types from module 'Client.netmodule'
foreach (var t in modules_A.Skip(1).First().Types)
{
if (t.Name == "<Module>")
continue;
// IL_0013: callvirt System.Void myStringer.Stringer::StringerMethod()
var callInst = t.Methods.First(m => m.Name.Equals("Main")).Body.Instructions[7];
if (callInst.Operand is not MethodReference methodReference)
continue;
// Type Name: myStringer.Stringer
// TypeRef Scope: Stringer.netmodule ScopeType:ModuleReference
var methodTypeRef = methodReference.DeclaringType;
var methodTypeDef = methodTypeRef.Resolve();
}
The line var methodTypeDef = methodTypeRef.Resolve();
fails with an NRE becuase:
methodTypeRef
's module is ‘Client.netmodule’- The ModuleDefinition of ‘Client.netmodule’ has no Assembly (
null
). According to ECMA this is valid. - The MetadataResolver implementation is as follows:
var modules = type.Module.Assembly.Modules;
As you see it assumes, a ModuleDefinition always has an Assembly assigned to it.
Solutions
I see a couple of solutions here.
- Check whether the module has an assembly
a. return
null
in that case, indicating that it was impossible to find a proper TypeDefinition b. Have some heuristic-based logic which tries to find a ModuleDefinition which matches the ModuleReferenc’s name. Either lookup the assembly resolver’s lookup directories for such a module or try on some cached ModuleDefinitions. Return null when no module was found or no module contained to TypeDefinition of the searched TypeReference. Or return the TypeDefinition if found. I think it should be noted however that this approach might return a wrong TypeDefinition if the wrong module matched. - Same as for 1. But instead of returning null, throw a proper exception.
There are probably more and smarter solutions but I think that’s up to you to decide @jbevain
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
c# - What is a NullReferenceException, and how do I fix it?
This means the reference is null , and you cannot access members (such as methods) through a null reference.
Read more >[Solved] How to resolve NullReferenceException
It simply means that some member/variable of some reference type is dereferenced by using and of its instance (non-static) members, which ...
Read more >How can I fix the error: System.NullReferenceException
A NullReferenceException exception is thrown when you try to access a member on a type whose value is null. A NullReferenceException exception ......
Read more >Object reference not set to an instance of an object
A null reference means that it is trying to access something that doesn't exist. You either forgot to drag something in the editor, ......
Read more >What is a Null Reference Exception?
A NullReferenceException happens when you try to access a reference variable that isn't referencing any object. If a reference variable isn't referencing an ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thank you, I’ll have a look!
(Using netmodules is not a good idea they’re just a source of pain on earth)
Fixed by #730