在委托中使用'base'关键字会导致System.BadImageFormatException

Ala*_*laa 5 c#-4.0

我有一个奇怪的问题,我想如果有人可以告诉我为什么会发生这种情况.我在基本抽象类中有一个受保护的方法,如下所示:

protected T ForExistingEntity<T>(TEntity entity, object key, Func<Entity, T> action) {
    entity = GetByKey(key);
    if (entity != null)
        return action(entity);

    return default(T); 
}
Run Code Online (Sandbox Code Playgroud)

我从继承类的原始调用如下:

return base.ForExistingEntity(
    new MyEntity(), key, e => {
        e.someFiled = 5;
        return base.Update(e);
    }
);
Run Code Online (Sandbox Code Playgroud)

执行此代码时,会在以下行中引发异常:

return action(entity);
Run Code Online (Sandbox Code Playgroud)

在基础抽象类中.例外是:

System.BadImageFormatException:尝试加载格式不正确的程序.(HRESULT异常:0x8007000B)

现在当我修改我的电话如下:

return base.ForExistingEntity(
    new MyEntity(), key, e => {
        e.someFiled = 5;
        return Update(e);
    }
);
Run Code Online (Sandbox Code Playgroud)

它运行正常,没有任何问题.

编辑:

Update方法位于基本抽象类中,如下所示:

public virtual bool Update(TEntity entity) {
    Condition.Requires(entity, "entity")
        .IsNotNull();

    if (ValidateEntity(entity))
        return Update(entity, true);

    return false;
}
Run Code Online (Sandbox Code Playgroud)

我开始认为这是因为Update是虚拟的并且调用实际上源于基类本身?无论如何,例外并不是很有帮助.

Jul*_*ain 8

这似乎是一个已知的C#编译器错误,涉及从泛型类中的匿名方法调用基本虚方法.如果你想解决它,请不要犹豫,在连接上提出这个错误.幸运的是,这里的解决方法非常简单.