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

NullReferenceException when calling methodDefinition.Body #34

Closed
madgnome opened this issue Sep 6, 2010 · 9 comments
Closed

NullReferenceException when calling methodDefinition.Body #34

madgnome opened this issue Sep 6, 2010 · 9 comments

Comments

@madgnome
Copy link

madgnome commented Sep 6, 2010

MethodDefinition.Body throws a NullReferenceException when called on a method of an interface generated for COM interop.
(_System.Void Microsoft.Office.Interop.Excel._Application::VtblGap1_15())

namespace ciproject4
{
  public class ComInterop
  {
    public static void main(String args)
    {
      var excel = new Microsoft.Office.Interop.Excel.Application();
      excel.Cells[1, 1].Value2 = "Ma feuille Excel";
    }
  }
}

4 interfaces are generated by the compiler for this program :

  • Microsoft.Office.Interop.Excel._Application
  • Microsoft.Office.Interop.Excel.Application
  • Microsoft.Office.Interop.Excel.AppEvents
  • Microsoft.Office.Interop.Excel.AppEvents_Event

_Application declaration looks like that :

[ComImport, TypeIdentifier, Guid("000208D5-0000-0000-C000-000000000046"), CompilerGenerated, DefaultMember("_Default")]
public interface _Application
{
  void _VtblGap1_15();
  Range Cells { [return: MarshalAs(UnmanagedType.Interface)] [DispId(238)] get; }
}

Stacktrace :

at Mono.Cecil.Cil.CodeReader.IsInSection(Int32 rva) in Mono.Cecil.Cil\CodeReader.cs:line 88
at Mono.Cecil.Cil.CodeReader.MoveTo(Int32 rva) in Mono.Cecil.Cil\CodeReader.cs:line 78
at Mono.Cecil.Cil.CodeReader.ReadMethodBody() in Mono.Cecil.Cil\CodeReader.cs:line 93
at Mono.Cecil.Cil.CodeReader.ReadMethodBody(MethodDefinition method) in Mono.Cecil.Cil\CodeReader.cs:line 71
at Mono.Cecil.MetadataReader.ReadMethodBody(MethodDefinition method) in Mono.Cecil\AssemblyReader.cs:line 1830
at Mono.Cecil.MethodDefinition.<get_Body>b__2(MethodDefinition method, MetadataReader reader) in Mono.Cecil\MethodDefinition.cs:line 152
at Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func`3 read) in Mono.Cecil\ModuleDefinition.cs:line 704
at Mono.Cecil.MethodDefinition.get_Body() in Mono.Cecil\MethodDefinition.cs:line 152
@jbevain
Copy link
Owner

jbevain commented Sep 6, 2010

Oh my. Where is the excell interop dll on your system?

@madgnome
Copy link
Author

madgnome commented Sep 6, 2010

C:\Program Files\Microsoft Visual Studio 10.0\Visual Studio Tools for Office\PIA\Office14

but the dll is not in the gac.

@jbevain
Copy link
Owner

jbevain commented Sep 6, 2010

Never mind, extracted one from a msi.

@madgnome
Copy link
Author

madgnome commented Sep 6, 2010

Same exception if the excell interop dll is in the gac or in cecil search directory.

@jbevain
Copy link
Owner

jbevain commented Sep 6, 2010

Somehow I can't seem to be able to compile a repro. Can you upload your dll somewhere?

@madgnome
Copy link
Author

madgnome commented Sep 6, 2010

Sent by mail.

@jbevain
Copy link
Owner

jbevain commented Sep 6, 2010

Thanks.

I have a different exception though. When running:

foreach (var type in module.Types) {
foreach (var method in type.Methods) {
if (method.HasBody) {
Console.WriteLine (method.Body);
}
}
}

I don't get any exception. However, I do get a NRE when trying to save this assembly.

@jbevain
Copy link
Owner

jbevain commented Sep 6, 2010

Fixed in master. Thanks!

@madgnome
Copy link
Author

madgnome commented Sep 6, 2010

Great! Thanks!

vitek-karas added a commit to vitek-karas/cecil that referenced this issue Jun 29, 2022
* ILProcessor should also update custom debug info

When eidting IL with ILProcessor various pieces of debug information have references to the origin IL instructions. These references can be either resolved (point to Instruction instance), in which case the editting mostly works, or unresolved (store IL offset only) in which case they need to be resolved before the editting can occur (after the edit the original IL offsets are invalid and unresolvable).

This is effectively a continuation of jbevain#687 which implemented this for local scopes. This change extends this to async method stepping info and state machine scopes.

The change refactors the code to make it easier to reuse the same logic between the various debug infos being processed.

Updated the existing tests from jbevain#687 to include async and state machine debug info (completely made up) and validate that it gets updated correctly.

* PR Feedback

Renamed some parameters/locals to better match the existing code style.

* PR Feedback
vitek-karas added a commit to vitek-karas/cecil that referenced this issue Jul 19, 2022
* ILProcessor should also update custom debug info

When eidting IL with ILProcessor various pieces of debug information have references to the origin IL instructions. These references can be either resolved (point to Instruction instance), in which case the editting mostly works, or unresolved (store IL offset only) in which case they need to be resolved before the editting can occur (after the edit the original IL offsets are invalid and unresolvable).

This is effectively a continuation of jbevain#687 which implemented this for local scopes. This change extends this to async method stepping info and state machine scopes.

The change refactors the code to make it easier to reuse the same logic between the various debug infos being processed.

Updated the existing tests from jbevain#687 to include async and state machine debug info (completely made up) and validate that it gets updated correctly.

* PR Feedback

Renamed some parameters/locals to better match the existing code style.

* PR Feedback
jbevain pushed a commit that referenced this issue Sep 29, 2022
* ILProcessor should also update custom debug info (#34)

* ILProcessor should also update custom debug info

When eidting IL with ILProcessor various pieces of debug information have references to the origin IL instructions. These references can be either resolved (point to Instruction instance), in which case the editting mostly works, or unresolved (store IL offset only) in which case they need to be resolved before the editting can occur (after the edit the original IL offsets are invalid and unresolvable).

This is effectively a continuation of #687 which implemented this for local scopes. This change extends this to async method stepping info and state machine scopes.

The change refactors the code to make it easier to reuse the same logic between the various debug infos being processed.

Updated the existing tests from #687 to include async and state machine debug info (completely made up) and validate that it gets updated correctly.

* PR Feedback

Renamed some parameters/locals to better match the existing code style.

* PR Feedback

* Fix test on Linux

Native PDB is not supported on Linux and the test infra falls back to portable PDB automatically. Since the two PDB implementations read the custom debug info from a different place the test constructing the input needs to adapt to this difference as well.
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants