Direct access to full string representation of object

◇◆丶佛笑我妖孽 提交于 2019-12-24 08:48:35

问题


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?


回答1:


.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.




回答2:


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.



来源:https://stackoverflow.com/questions/8973200/direct-access-to-full-string-representation-of-object

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!