hazzik / DelegateDecompiler

A library which is able to decompile a delegate or a method body to its lambda representation
MIT License
522 stars 62 forks source link

Null reference error in TypeHierarchy.BuildChildren #218

Closed fimiki closed 1 year ago

fimiki commented 1 year ago

DelegateDecompiler is throwing an "Object reference not set to an instance of an object." error when I attempt to decompile a method.

  at DelegateDecompiler.TypeHierarchy.BuildChildren(Type type, ILookup`2 children)
   at DelegateDecompiler.TypeHierarchy.Traverse(ICollection`1 result, HashSet`1 usher, Type type, ILookup`2 children)
   at DelegateDecompiler.TypeHierarchy.Traverse(Type root, IEnumerable`1 descendants)
   at DelegateDecompiler.MethodBodyDecompiler.DecompileVirtual(Type declaringType, MethodInfo method, IList`1 args)
   at DelegateDecompiler.MethodBodyDecompiler.Decompile(MethodInfo method, Type declaringType)
   at DelegateDecompiler.DecompileExtensions.<>c__DisplayClass7_0.<.cctor>b__1()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at DelegateDecompiler.DecompileExtensions.Decompile(MethodInfo method, Type declaringType)
   at DelegateDecompiler.DecompileExpressionVisitor.Decompile(MethodInfo method, Expression instance, IList`1 arguments)
   at DelegateDecompiler.DecompileExpressionVisitor.VisitMethodCall(MethodCallExpression node)
   at System.Linq.Expressions.ExpressionVisitor.VisitLambda[T](Expression`1 node)
   at DelegateDecompiler.DecompileExpressionVisitor.Decompile(Expression expression)

For some reason, in my application, something within the reflection process used by DelegateDecompiler's MethodBodyDecompiler.DecompileVirtual generated two different references to the same root interface where the method signature was declared. This meant that a type with a null BaseType property was included in the list of descendants, eventually yielding the null reference error in BuildChildren. There are four different assemblies in play, which makes the exact conditions a bit of a challenge to replicate and test. Fortunately, the fix looks pretty straightforward!