直接访问对象的完整字符串表示 [英] Direct access to full string representation of object
问题描述
我正在尝试将对象的内容记录到文本文件中.如果我在即时窗口中对对象本身执行 debug.print
,它会打印对象属性的所有值:
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
但是,由于类型不匹配,我似乎无法在代码中将其作为字符串访问.我假设我可以使用 .ToString
方法获取此信息,但返回的只是没有任何属性或值的对象描述:
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"
我错过了什么?
推荐答案
.ToString 是基础对象上的函数(请参阅 http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx).Debug.Write 是一个可以遍历写入值的属性的函数.
.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.
正如 Stu 所说,您可以使用 Reflection 自己做到这一点.
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.
这篇关于直接访问对象的完整字符串表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!