mercredi 20 novembre 2019

Emit Reflection, Breaks when expanding Parent Class Generic Params

I have created working Dynamic Controllers which I'm creating through reflections. They all derrive from the BaseType DynamicRestController:

public class DynamicRestController<TEntity, TDTO, TKey>
: AsyncCRUDSingleKeyController<TEntity, TDTO, TKey>>
    where TKey : struct
{
    public DynamicRestController() : base(null) {}
}

My Controllers are designed to take in a service type by default but to get the dynamic controllers working I hide the constuctor

public class AsyncCRUDSingleKeyController<TEntity, TDTO, TKey>
  :  AsyncCRUDSingleKeyController<TEntity, TDTO, TKey>
{
    public AsyncCRUDSingleKeyController(IRestEzService<TEntity, TDTO> service) 
        : base(service)
    {
    }
}

public class AsyncCRUDSingleKeyController<TEntity, TDTO, TKey, TService> : Controller
    where TKey: struct
    where TService: IRestEzService<TEntity, TDTO>
{
    protected TService _service;
    public AsyncCRUDSingleKeyController(TService service)
    {
        this._service = service;
    }
    //...
}

Whats weird is when I change the DynamicRestController to use its base directly my controllers 404 with no exceptions e.g

public class DynamicRestController<TEntity, TDTO, TKey>
  : AsyncCRUDSingleKeyController<TEntity, TDTO, TKey, IRestEzService<TEntity, TDTO>>
    where TKey : struct
{
    public DynamicRestController() : base(null) {}
}

From a generation standpoint nothing should have changed aside relative to the code generation e.g

internal Type CreateNewControllerType<TEntity, TDTO, TKey>(string typeName, 
        List<DynamicMethodGeneratorOptions> methodGeneratorOptions)
        where TKey : struct
    {
        var baseType = typeof(DynamicRestController<TEntity, TDTO, TKey>);
        var typeBuilder = this.GetTypeBuilder(this._moduleBuilder, typeName);
        typeBuilder.SetParent(baseType);
        var ctorMethodAttrs = MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName;
        typeBuilder.DefineDefaultConstructor(ctorMethodAttrs);
        foreach (var methodOption in methodGeneratorOptions)
        {
            var getMethodBuilder = this.DefineMethod(typeBuilder, methodOption.MethodDescriptor);
            var methodIlGenerator = getMethodBuilder.GetILGenerator();
            methodOption.ApplyMethodDefinition(methodIlGenerator);
        }
        return typeBuilder.CreateType();
    }

No Error is generated during dynamic compilation, only a 404 is thrown Why would changing a non dependent class break the ability to load the controller?





Aucun commentaire:

Enregistrer un commentaire