相关疑难解决方法(0)

外变量陷阱

外部变量陷阱究竟是什么?感谢C#中的解释和示例.

编辑:纳入Jon Skeet的diktat :)

Eric Lippert关于外部变量陷阱

c# linq

46
推荐指数
2
解决办法
8324
查看次数

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

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

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)

c#-4.0

5
推荐指数
1
解决办法
479
查看次数

标签 统计

c# ×1

c#-4.0 ×1

linq ×1