question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullReferenceException when trying to resolve ModuleReference members

See original GitHub issue

The 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.

  1. 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.
  2. 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:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
jbevaincommented, Mar 15, 2021

Thank you, I’ll have a look!

(Using netmodules is not a good idea they’re just a source of pain on earth)

0reactions
jbevaincommented, Apr 23, 2021

Fixed by #730

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found