0

I've tried to gather as much info as possible, when my VB.Net app crashes.

I have some objects that I need to convert to string, so I can send the objects value to me in an email.

How can I convert an object to string - even if they are not serializable?

In my test, if I try to serialize my test object, I get an error, since it inherits from IDictionary, it can't be serialized. But I can add it to the "Watch" window and still see all its properties. So there must be a way to convert an non serializable object to a string?

3
  • 1
    So, are you basically asking for a way to serialize an object? Why don't you try to serialize it to Json, for example. I'm sure it can deal with IDictionary types. Give it a try: json.codeplex.com
    – dandel
    Aug 30, 2013 at 9:06
  • Depending on what you understand by "app crash" - here may be info how to log Exceptions that cause the App to crash (unhandled Exceptions) - (stackoverflow.com/questions/13895929/…) and just log The Exception with a Logging Framework (which can be configured to send errors via mail, log them to Filesystem...) Aug 30, 2013 at 9:08
  • @dandel put it as an answer and I will mark it :)
    – MojoDK
    Aug 30, 2013 at 13:00

3 Answers 3

2

I had such question before - Logging instance data

So you can use reflection and go through properties by your own or you can take something, that is already written, for example ServiceStack has Dump method: http://www.servicestack.net/mythz_blog/?p=202

BTW, not sure what you mean by "not serializable". If object can't be serialized to xml using default .net classes, that doesn't mean it can't be serialized using custom classes or different serialization format, like JSON or binary.

3
  • This looks good, but if I have like this ... Public Function SerializeObject(o As Object) As String ... then o.Dump fails with member Dump is not found in the object. Any how to fix this?
    – MojoDK
    Aug 30, 2013 at 12:33
  • you need to install package nuget.org/packages/ServiceStack.Text and add using statment: using ServiceStack.Text;
    – Giedrius
    Aug 30, 2013 at 19:18
  • I did that and if I do MyObject.Dump, it works, but if I send the object as a parameter to my function above ... Call SerializeObject(MyObject) ... then my functions method ... o.Dump ... fails.
    – MojoDK
    Aug 30, 2013 at 19:44
1

You can do it by using reflection

Reflection (C# Programming Guide)

1
  • link-only answers are discouraged. You'd better provide some extra explanation or examples. Aug 30, 2013 at 11:59
0

So, are you basically asking for a way to serialize an object? Why don't you try to serialize it to Json, for example. I'm sure it can deal with IDictionary types. Give it a try: json.codeplex.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.