将字符串转换为变量

发布于 2024-12-12 01:30:55 字数 234 浏览 0 评论 0原文

这可能不可能,但对调试非常有帮助。有时程序运行时会出现随机错误。我希望能够在(字符串)中输入一些内容,然后它将获取具有该字符串名称的变量并返回其值。

int mainNumber = 89;

输入:检索mainNumber 输出:89

这样的事情可能吗?我不想为我拥有的每个变量创建调试,以免出现问题。我知道我可以等待错误发生,停止程序,进行调试并再次等待,但这会更快。

This might not be possible but it would be hugely helpful with debugging. Sometimes random bugs occur when the program is running. I'd like to be able to type something in (a string) and then it would grab a variable with the name of that string and return its value.

int mainNumber = 89;

Input: retrieve mainNumber
Output: 89

Is something like that possible? I don't want to have to create debugs for every single variable I have on the off chance something could go wrong. I know I could wait for the bug to occur, stop the program, throw a debug in and wait again, but this would be faster.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初懵 2024-12-19 01:30:56

我建议阅读 .NET 中的跟踪和调试。您通常可以切换 Trace从程序的配置文件中关闭和打开调试语句,因此不会影响您的运行时表现除非你想要这些转储。然后,您可以在代码中添加 Trace.Write(...)Debug.Write(...) 语句来提供您想要的输出。

I would suggest reading up on Trace and Debug in .NET. You can typically switch Trace and Debug statements off and on from a program's config file, thus not affecting your runtime performance unless you want these dumps. Then you can sprinkle Trace.Write(...) and Debug.Write(...) statements in your code to provide the output you desire.

谁的年少不轻狂 2024-12-19 01:30:56

根据您的评论,听起来您需要做一些研究。以下是一些需要考虑/尝试的事情:

  1. bug 是什么?是否抛出了异常,数据是否未正确返回,等等。确定您试图解决的根本原因是什么。
  2. 你知道什么情况下会出现这个bug吗?既然你说随机,那么你很可能还不知道答案。
  3. 您能够在测试环境中重现或看到该错误吗?

我过去做过的一种选择是将日志记录(通常是文本文件)添加到相关应用程序中。我在感兴趣的领域进行了日志记录,并为条目添加了时间戳。获取足够的信息,以便您可以充分识别正在发生的操作(变量/对象的状态之前/之后,识别操作的任何支持信息等)。

当报告(或重现)错误时,如果报告者提供了足够的信息(即时间、他们在做什么、应用程序特有的其他事情或他们在做什么),您可以查看日志文件并查看发生了什么在。

一旦掌握了这些信息,您就可以确定根本原因,或者将范围缩小到更具体的领域并集中精力在那里。

这可能是一个迭代过程,具体取决于应用程序有多大以及可能出现问题的区域有多大。

Based on your comment, it sounds like you're going to need to do some research. Here are some things to consider/try:

  1. What is the bug? Is an exception being thrown, is data not coming back correctly, etc. Identify what it is that you're trying to root cause.
  2. Do you know under what circumstances the bug happens? Since you said random, chances are you don't yet know the answer to that.
  3. Are you able to reproduce or see the bug in a test environment?

One option that I've done in the past is to add logging (usually to a text file) to the application in question. I put in logging in the areas of interest, and timestamp the entries. Grab enough information so you can adequately identify the actions that are occurring (before/after states of variables/objects, any supporting information that identifies the action, etc).

When a bug is reported (or reproduced), if the reporter gives enough information (i.e., time, what they were doing, other things particular to the app or what they were doing) you can look in your log files and see what was going on.

Once you have that information, you can either identify the root cause, or narrow it down to a more specific area and focus your efforts there.

It can be something of an iterative process, depending on how big the app is and how large the area of possible problems can be.

獨角戲 2024-12-19 01:30:55

您需要了解变量观看立即 Visual Studio 窗口。

You need to learn about the variable, watch and immediate windows of Visual Studio.

不可一世的女人 2024-12-19 01:30:55

“立即窗口”是您所寻找的吗? http://msdn.microsoft.com/en-我们/库/f177hahy(v=VS.100).aspx

Would the 'immediate window' be what you're looking for? http://msdn.microsoft.com/en-us/library/f177hahy(v=VS.100).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文