C# 和 .NET 语言:取代 C 宏的使用
我是 .Net 开发的新手,我正在寻求一些建议。
我有一些代码会引发一些异常。例如,如果我的数据库关闭,则会引发这些异常。我想让用户能够在失败时重试执行的代码。
这是我的初始代码:
executeRequest();
我在 C 风格中的想法(但无论如何在 C# 中工作):
do
{
try
{
executeRequest();
return;
}
catch (Exception exception)
{
ErrorMessagesUtils.ShowTaskErrorMessage(exception);
}
} while (MessageBox.Show("Failed : Retry ?", "Error",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes);
它正在工作,但用其他方法重现它真的很烦人。我想与executeRequest一起使用,但不仅。
如果我用 C 语言编码,我将使用 #define ASK_USER_TO_RETRY(funcname) ...code...
。但在 C# 中,我还没有找到以这种方式重用一段代码的方法。有些想法?
I'm new in .Net development, and I'm looking for some advices.
I've some piece of code which throw some exceptions. These exception are thrown if my database is down for example. I want to let users have the capability to retry the executed code if failed.
Here my initial code :
executeRequest();
What I've thinked in C-style (but working anyway in C#):
do
{
try
{
executeRequest();
return;
}
catch (Exception exception)
{
ErrorMessagesUtils.ShowTaskErrorMessage(exception);
}
} while (MessageBox.Show("Failed : Retry ?", "Error",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question) == DialogResult.Yes);
It's working but it's really annoying to reproduce it with other methods. I want to use with executeRequest but not only.
If I was coding in C, I'll use #define ASK_USER_TO_RETRY(funcname) ...code...
. But in C#, I've not found a way to reuse a piece of code in this manner. Some thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以创建方法,该方法接受委托(要执行的方法):
用法:
You can create method, which accepts delegate (method to be executed):
Usage:
看到用户不断地敲击重试按钮却没有任何进展,这总是一个非常悲伤的景象。他们也坚持了一段时间,希望有奇迹出现。半分钟后他们会将鼠标切换到左手。
这样做的普遍问题是他们会因为这个问题而责备你。即使你与它无关并且根本无法做任何事情来解决问题。毕竟,是你承诺重试是有意义的。他们很快就会对你作为程序员以及你的产品的可用性和物有所值失去信心。除非您实际上可以在代码中执行不同的策略,否则不要这样做。这是相当罕见的。
It is always a pretty sad sight to watch a user banging away at a retry button without ever getting anywhere. They keep it up for a while too, hoping for a miracle. They'll switch the mouse to the left hand after half a minute of it.
The generic problem with this is that they'll blame you for the problem. Even though you have nothing to do with it and cannot do anything at all to solve the problem. After all, it was you that promised that a retry would make sense. They'll quickly lose confidence in you as a programmer and your product as being usable and worth their money. Don't do this unless you actually have a way in your code to execute a different strategy. That's quite rare.
即使在 C 语言中使用宏也可能是个坏主意。如您所知,使用预处理器您甚至看不到编译器看到的内容。我认为函数指针更适合这种情况。
因此,正如其他人提到的,在 C# 中,函数指针被委托替换。如果愿意,您可以将它们视为类型安全函数指针。
我会推荐 Pro C# 2010 和 .NET 4 Platform 和 C# 语言规范
Even in C using a macro for this maybe a bad idea. As you know, with the pre-processor you don't even see what the compiler sees. I'd assume a function pointer would be a better fit in that scenario.
So, as others have mentioned, in C# function pointers are replaced with delegates. You can think of them as type-safe function pointers if you will.
I would recommend Pro C# 2010 and the .NET 4 Platform and C# Language Specification