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

Create type in separate netmodule not supported #818

Open
kant2002 opened this issue Dec 12, 2021 · 0 comments · May be fixed by #819
Open

Create type in separate netmodule not supported #818

kant2002 opened this issue Dec 12, 2021 · 0 comments · May be fixed by #819

Comments

@kant2002
Copy link

Take a look at this snippet

using Mono.Cecil;

var fooModule = ModuleDefinition.CreateModule("Foo", ModuleKind.NetModule);
var foo = new TypeDefinition("", "Foo", 0, fooModule.TypeSystem.Object);
fooModule.Types.Add(foo);

var mainModule = ModuleDefinition.CreateModule("Main", ModuleKind.NetModule);
mainModule.ModuleReferences.Add(fooModule);
var program = new TypeDefinition("", "Program", TypeAttributes.Class, mainModule.TypeSystem.Object);
program.Fields.Add(new FieldDefinition("foo", FieldAttributes.Static, mainModule.ImportReference(foo)));
mainModule.Types.Add(program);

Right now Cecil crashes with

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at Mono.Cecil.DefaultMetadataImporter.ImportScope(IMetadataScope scope)
   at Mono.Cecil.DefaultMetadataImporter.ImportScope(TypeReference type)
   at Mono.Cecil.DefaultMetadataImporter.ImportType(TypeReference type, ImportGenericContext context)
   at Mono.Cecil.DefaultMetadataImporter.ImportReference(TypeReference type, IGenericParameterProvider context)
   at Mono.Cecil.ModuleDefinition.ImportReference(TypeReference type, IGenericParameterProvider context)
   at Mono.Cecil.ModuleDefinition.ImportReference(TypeReference type)

This fails at this line

return ImportReference (((ModuleDefinition) scope).Assembly.Name);

when attempt to get Name from Assembly which is not defined in this case.

So apparently Cecil not very well supports creation of netmodules.

@kant2002 kant2002 linked a pull request Dec 13, 2021 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant