使用我自己的自定义类库进行调试 [英] Debugging with my own custom Class Libraries

查看:62
本文介绍了使用我自己的自定义类库进行调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有条件地编译我的类库,以便在我的项目时它处于调试模式,而不在我的项目时它不在调试模式.

I want my class library conditionally compiled so that it is in debugging mode when my project is, and is not when my project isn't.

例如,我的班级库中有这个模块:

For example, I have this Module in my class library:

Module MyDebug

    <Conditional("DEBUG")>
    Sub print(ByVal msg As String)
        Debug.Print(Now.ToString("yyyy-MM-dd HH:mm:ss.fff") & " " & msg)
    End Sub

    <Conditional("DEBUG")>
    Sub debugEnd(Byval bool As Boolean)

        Environment.Exit(0)
    End Sub
End Module

当我调试引用该库的项目时,它在调用它们时会运行其中的任何一个.

When I debug my project which references this library it run any of these when they are called.

我尝试过在线搜索,但是我找不到任何有用的东西,因为我只能找到与调试实际的类库有关的东西,而我只希望它在调试时发送这些条件编译的语句我的项目.

I've tried searching online, but I haven't found anything that helps since I can only find things to do with debugging the actually class library, while I only want it to send these conditionally compiled statements while I'm debugging my project.

但是,通过一些试验,我发现如果在类库中,我将转到:

However, by experimenting around a bit, I've found that if in the class library I go to:

我的项目"->编译"标签->高级编译选项"

,然后勾选定义DEBUG常量"(然后构建库),项目在调用时确实会运行调试语句.

and then tick 'Define DEBUG constant' (and then build the library), the project does run the debug statements when called.

但是,我不确定类库中定义DEBUG常量"的行为.仅当我的项目处于调试模式时,它才定义DEBUG吗?

However, I'm not entirely sure of the behaviour of 'Define DEBUG constant' in the class library. Does it define DEBUG if and only if my project is in debug mode?

如果没有,那么有没有一种简单的方法可以实现我的目标?(我不想每次在项目中的调试和发行版之间切换时都选中/取消选中类库中的复选框,而且无论如何我的类库也被多个项目引用)

If not, then is there a simple way to acheive what I aim to do? (I don't want to have to tick/untick the checkbox in the class library each time I switch between debug and release in my project, and my class library is referenced by more than one project, anyway)

请注意,在项目中,我引用的是 Bin->中的 .dll .我的类库的Release 文件夹,希望它是引用它的正确方法.

Note that in the project I'm referencing the .dll in the Bin -> Release folder of my class library which I hope is the right way to reference it.

此外,我想问一下VB如何通过 Debug 类实现此目的,因为它也像其他任何类库一样通过引用导入,并且以我希望的方式工作上面的-当然可以做同样的事情?

In addition, I would like to ask about how VB acheives this with the Debug class, because it is also imported with a reference like any other class library, and works in the way I would like the above to — surely I could do the same?

推荐答案

如果将类库项目保留在与主项目相同的解决方案中,它将使用相同的配置( Debug 发布)作为该解决方案中的所有其他项目.这意味着您无需手动检查/取消选中 Define DEBUG常量,因为如果将配置设置为 Release ,则无论如何都不会定义它.

If you keep the class library project in the same solution as your main project it will use the same configuration (Debug or Release) as every other project in that solution. This means that you won't have to manually check/uncheck Define DEBUG constant as it will not be defined anyway if you have set the configuration to Release.

即使您的项目不在同一文件夹中,您仍可以将类库项目添加到解决方案中.操作方法如下:

Even if your projects are not in the same folder you may still add your class library project to your solution. Here's how to do it:

  1. Solution Explorer 中右键单击您的解决方案,然后转到 Add>现有项目.

  1. Right-click your solution in the Solution Explorer and goto Add > Existing Project.

在打开的文件浏览对话框中,找到类库的 .vbproj 文件,然后单击 OK .

In the file browsing dialog that opens, locate the .vbproj-file of your class library and click OK.

现在,当您更改编译配置时,它应该反映在整个解决方案中,并且如果您将配置设置为 Debug ,则您的类库应仅定义其 DEBUG 常量.代码>.

Now when you change compilation configuration it should be reflected over the entire solution and your class library shall only have its DEBUG constant defined if you set the configuration to Debug.

如果您无法在 Solution Explorer 中看到您的解决方案:

If you cannot see your solution in the Solution Explorer:

  1. 在Visual Studio中,转到 Tools>选项>项目和解决方案常规.

选中始终显示解决方案,然后按 OK .


要使上述方法起作用,您还必须更改引用类库的方式.您目前的操作方式是正确的,但在这种情况下将无法正常工作,因为您只能引用dll的 Release 版本.

  1. 从删除当前对类库的引用开始.

  1. Start by removing your current reference to your class library.

打开"添加引用"对话框,然后转到"项目"选项卡.

Open the Add Reference dialog and go to the Projects tab.

选择您的类库的项目,然后按 OK .

Select your class library's project and press OK.

这现在应该引用解决方案当前编译配置中类库的输出.

This should now reference the class library's output from the solution's current compilation configuration.

这篇关于使用我自己的自定义类库进行调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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