什么是c#dll向调用应用程序返回错误的好方法? [英] What's a good way for a c# dll to return error to the calling application?

查看:143
本文介绍了什么是c#dll向调用应用程序返回错误的好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个dll,它是一个访问数据库的包装器。我一直很喜欢c#,因为我的背景是在web开发LAMP与perl,我不知道什么是一个很好的方法来返回错误到一个调用应用程序,以防万一他们将错误的参数传递给我的功能或什么不是



我现在不知道除了大概做一些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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆