JasperFx / lamar

Fast Inversion of Control Tool and Successor to StructureMap
https://jasperfx.github.io/lamar
MIT License
572 stars 119 forks source link

Virtual Methods : Frames "Cannot be an empty list" exception #185

Closed seankain closed 4 years ago

seankain commented 5 years ago

I was thinking of generating AspNetCore.Mvc Controllers. I got an exception thrown from line 135 of GeneratedMethod.cs when the GeneratedMethod.Frames is empty. I found that it appeared to be related to virtual methods. To test:


public class TestControllerBase
{ 
   public virtual string Get() { return "Got"; }
}

[Fact]
public void GenerateControllerAssembly()
{
    var assembly = new GeneratedAssembly(rules);
    var testController = new GeneratedType("TestController"); 
    imageController.InheritsFrom<TestControllerBase>();
    assembly.GeneratedTypes.Add(testController);
    new AssemblyGenerator().Compile(assembly);
    var controllerInstance = 
    Activator.CreateInstance(imageController.CompiledType).As<TestControllerBase>();
    controllerInstance.Get().ShouldBe("Got")
}

As a test hack I commented out line 40 of ReflectionExtensions regarding virtual and final access modifiers. It allowed me to get past the issue but would probably cause severe issues with overrides. I am new to Lamar and just wanted to check if this is truly a bug or if it is just something that I am missing.

jeremydmiller commented 5 years ago

Sorry, just now saw this.

So, yeah, commenting out the line 40 would definitely break a lot of other functionality. The codegen model just needs to understand that if there's a virtual method with no Frames, it should be skipped in the code gen instead of blowing up.

It's not a scenario I envisioned or tested for obviously.

jeremydmiller commented 4 years ago

Finally got this today.