直接访问对象的完整字符串表示形式

发布于 2024-12-28 06:50:18 字数 465 浏览 5 评论 0原文

我正在尝试将对象的内容记录到文本文件中。如果我在立即窗口中对对象本身进行 debug.print 操作,它会打印对象属性的所有值:

?mDb.DatabaseOptions
{Microsoft.SqlServer.Management.Smo.DatabaseOptions}
    AnsiNullDefault: False
    ...
    UserData: Nothing

但是,我似乎无法在代码中将其作为字符串访问,因为类型不匹配。我假设我可以使用 .ToString 方法获取此信息,但返回的只是没有任何属性或值的对象描述:

?mDb.DatabaseOptions.ToString
"Microsoft.SqlServer.Management.Smo.DatabaseOptions"

我缺少什么?

I am trying to log the contents of an object to a text file. If I do a debug.print of the object itself in the immediate window, it prints all of the values of the object's properties:

?mDb.DatabaseOptions
{Microsoft.SqlServer.Management.Smo.DatabaseOptions}
    AnsiNullDefault: False
    ...
    UserData: Nothing

However, I can't seem to access this as a string in code due to a type mismatch. I assumed I could get this information using the .ToString method, but all that returns is the object description with none of the properties or values:

?mDb.DatabaseOptions.ToString
"Microsoft.SqlServer.Management.Smo.DatabaseOptions"

What am I missing?

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

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

发布评论

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

评论(2

烟织青萝梦 2025-01-04 06:50:18

.ToString 是基础对象上的函数(请参阅 http:// msdn.microsoft.com/en-us/library/system.object.tostring.aspx)。 Debug.Write 是一个可以迭代写入值的属性的函数。

正如 Stu 所说,您可以使用反射自己完成此操作。

您还可以添加/更改跟踪侦听器以在其他位置写出信息。

.ToString is a function on the base object (see http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx). Debug.Write is a function that can iterate though the properties writing the values.

As Stu said you can do this yourself using Reflection.

You could also add/change the trace listeners to write out the information else where.

喜爱纠缠 2025-01-04 06:50:18

Debug.Print 为您枚举所有属性。这就是您要找的吗?如果是这样,您将必须通过反射检查所有属性。

Debug.Print enumerates all properties for you. Is that what you are looking for? If so, you will have to examine all properties through reflection.

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