问题
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