使用“base”时,基类方法中的代码不执行关键词
当重写抽象方法并尝试调用我当前正在重写的基类的方法时,我看到一个非常奇怪的问题。
//In Dll "A"
namespace Rhino.Etl.Core.Operations
{
using System;
using System.Collections;
using System.Collections.Generic;
public class Row {}
public interface IOperation
{
IEnumerable<Row> Execute(IEnumerable<Row> rows);
}
public abstract class AbstractOperation : IOperation
{
public abstract IEnumerable<Row> Execute(IEnumerable<Row> rows);
}
public abstract class AbstractDatabaseOperation : AbstractOperation
{
}
public abstract class SqlBulkInsertOperation : AbstractDatabaseOperation
{
public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
{
Console.WriteLine("SqlBulkInsertOperation");
return rows;
}
}
}
//In console app "B"
namespace MyStuff
{
using System;
using System.Collections;
using System.Collections.Generic;
class Program
{
static void Main(string[] args)
{
ActualEtlOperation e = new ActualEtlOperation();
e.Execute(new Row[0]);
Console.ReadLine();
}
}
public abstract class SqlBulkInsertWithTruncateOperation : SqlBulkInsertOperation
{
public override IEnumerable<Row> Execute(IEnumerable<Row> rows)
{
Console.WriteLine("Truncate");
base.Execute(rows);
return rows;
}
}
public class ActualEtlOperation : SqlBulkInsertWithTruncateOperation
{
}
}
基本上,SqlBulkInsertOperation 并没有做我需要它做的事情,所以我需要在通过覆盖它来调用 Execute(rows) 之前和之后做一些工作。但是SqlBulkInsertOperation.Execute(Rows)中的代码没有执行。
在 Visual Studio 的调试器中运行此代码时,调试器不会执行该代码。当我将鼠标悬停在 Visual Studio 编辑器中的“base”上时,它知道基类的类型为 SqlBulkInsertOperation。
我缺少什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
编辑:我发现了问题......讽刺的是,我对 Eric 的“心灵调试”评论并不是那么遥远,因为 这篇博文。这是一个简短但完整的程序,它将演示正在发生的事情...
它将打印“In MyOverride.Execute”,但它*不会“打印“In SqlBulkInsertOperation.Execute”...因为该方法是使用迭代器块实现的 当然,
您可以更简单地演示这一点:
没有任何东西使用该方法的返回值 - 它被传递回
Main
,但没有任何东西调用GetEnumerator()
,然后MoveNext()
结果......因此该方法的主体永远不会执行。 com/Articles/Chapter6/IteratorBlockImplementation.aspx" rel="noreferrer">我关于迭代器块的文章,Eric 博客文章的第二部分,或从 《C# 深度深度学习》第一版主页(第 6 章介绍迭代器块,并且免费),了解更多详细信息。
EDIT: I've found the problem... and ironically, my "psychic debugging" comment to Eric wasn't so far off, given this blog post. Here's a short but complete program which will demonstrate what's going on...
That will print "In MyOverride.Execute" but it *won't" print "In SqlBulkInsertOperation.Execute"... because that method is implemented with an iterator block.
Of course, you can demonstrate this much more simply:
Nothing is using the return value of the method - it's being passed back to
Main
, but nothing ever callsGetEnumerator()
on it, and thenMoveNext()
on the result... so the body of the method is never executed.See my article on iterator blocks, part two of Eric's blog post, or download chapter 6 from the home page of the first edition of C# in Depth (chapter 6 covers iterator blocks, and is free) for more details on this.
这是我运行的代码:
运行良好;它产生“覆盖”和“基础”。
告诉您什么:修改我在此处发布的程序,以便它产生您遇到的问题,我们将对此进行分析。当您无法真正看到有问题的真实代码时,分析问题是非常困难的。
Here's the code I ran:
It runs fine; it produces "override" and "base".
Tell you what: modify the program I've posted here so that it produces the problem you are experiencing, and we'll analyze that. It is very difficult to analyze a problem when you cannot actually see the real code that is having problems.
对我来说效果很好。您的代码需要进行一些清理 - 您确定正在编译并执行此代码,而不是调试旧版本的代码吗?
Works fine for me. Your code needs a little cleanup - are you sure you're compiling and executing this code, and not debugging an older build of your code?
您的示例无法编译,缺少一些返回,但以下修改后的示例
调用如下:
生成输出
您必须执行示例中未包含的其他操作。
Your example does not compile, some returns are missing, but the following modified example
Called like this:
generates the output
You must have done something else not included in your example.
这些都是同时编译的吗?您可能会遭受断开连接的中间基类 http://blogs.msdn.com/b/ericlippert/archive/2010/03/29/putting-a-base-in-the-middle.aspx
are these all being compiled at the same time? you might be suffering from the disconnected middle base class http://blogs.msdn.com/b/ericlippert/archive/2010/03/29/putting-a-base-in-the-middle.aspx