什么是c#dll向调用应用程序返回错误的好方法? [英] What's a good way for a c# dll to return error to the calling application?
问题描述
我现在不知道除了大概做一些msgbox或抛出一些例外,但我不知道从哪里开始看。任何帮助或资源将不仅仅是有用的:)
谢谢〜
您可能不想在您的dll中显示消息对话框,这是客户端应用程序的工作,作为表示层的一部分。
.Net库程序集通常会将主机应用程序的异常引发起来,这就是我所看到的方法。
public static class LibraryClass
{
public static void DoSomething(int positiveInteger)
{
if(positiveInteger< 0)
{
throw new ArgumentException( ,positiveInteger);
}
}
}
然后由你的主机应用程序来处理这些异常,适当地记录和显示它们。
try
{
LibraryClass.DoSomething (-3);
}
catch(ArgumentException argExc)
{
MessageBox.Show(发生错误:+ argExc.ToString());
}
i'm writing a dll which is a wrapper to a access database. and i'm pretty new to c# in general as my background is in web development LAMP with perl, i'm not sure what's a good way to return error to a calling app in case they pass the wrong parameters to my functions or what not.
I have no idea as of now except to probably do some msgbox or throw some exceptions but i don't know where to start looking. Any help or resources would be more than useful :)
thanks~
You probably don't want to display message dialogs from within your dll, that's the job of the client application, as part of the presentation layer.
.Net library assemblies typically bubble up exceptions to the host application, so that's the approach I'd look at.
public static class LibraryClass
{
public static void DoSomething(int positiveInteger)
{
if (positiveInteger < 0)
{
throw new ArgumentException("Expected a positive number", "positiveInteger");
}
}
}
Then it's up to your host application to handle those exceptions, logging and displaying them as appropriate.
try
{
LibraryClass.DoSomething(-3);
}
catch(ArgumentException argExc)
{
MessageBox.Show("An Error occurred: " + argExc.ToString());
}
这篇关于什么是c#dll向调用应用程序返回错误的好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!