Skip to content

Create type in separate netmodule not supported #818

Open
@kant2002

Description

@kant2002

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions