On exception - convert object to string? - c#

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?

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.

You can do it by using reflection
Reflection (C# Programming Guide)

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

Related

Protobuf cannot serialize system.object

I have classes that have all the required attributes for serialization, and I'm storing them in system.object so that when the other end deserializes them, it can pattern match them and do different stuff depending on the object type.
object data = MyObject;
Serializer.SerializeWithLengthPrefix(stream, data, PrefixStyle.Fixed32); // exception happens here
Tried it with other serializing methods like XML/BinaryFormatter and it didn't seem to have a problem.
The exception is very descriptive here - protobuf-net can not serialize system.object as it has not been told how to serialize/deserialize it. This is not a limitation of protobuf-net, protobuf (the spec) is a contract serialization method so it requires a specification for the type in order to serialize it.
One option is to create a base class that is marked with ProtoContract attribute and inherit from there. Be sure to include all subclasses in the base class with ProtoInclude-attribute, otherwise the serializer won't know how to serialize the subclasses.
I would advice not doing this, as serialization of subclasses is sort of add on protobuf-net provides on top of the standard protobuf behaviour, and creates problems if you need to work with other libraries and/or languages. Instead I'd advice you to create a messaging format where you encode the type separately of the object (message in protobuf terms). For example encode the type as enum as the first 4 bytes in the beginning of the message followed by the object itself. Then you can first read the type and then deserialize accordingly, without having to resort to subclasses.

C# Protobuf, submitting generic type information

I want to serialize C# objects using Protobuf, send them via network and deserialize them on the "other" side.
So Client A has e.g. the following Code:
Serializer.Serialize<ClassA>(stream, instance);
Now, I want to be able to transmit the typeinformation being "ClassA", too. On the receiving side, Client B has to call:
ClassA instance = Serializer.Deserialize<ClassA>(stream);
Is there an elegant way to directly transmit the generic typeinformation? The only other way that I could think of is to define a gigantic enum, containing all types that can be serialized and put this Information together with the actual instance of ClassA in a wrapper class.
Hints or possible solutions are very much appreciated!
Json.Net can serialize the objects into json, and if you give it the correct hints, it will send through the binary information required to deserialize it on the other side.
If you look at the example here: http://www.newtonsoft.com/json/help/html/SerializeTypeNameHandling.htm
They use a List, where the T is the generic parameter. So in your case T would be something else, but the principle is the same.

Why do you have to mark a class with the attribute [serializable]?

Seeing as you can convert any document to a byte array and save it to disk, and then rebuild the file to its original form (as long as you have meta data for its filename etc.).
Why do you have to mark a class with [Serializable] etc? Is that just the same idea, "meta data" type information so when you cast the object to its class things are mapped properly?
Binary serialization is pretty powerful, it can create an instance of a class without running the constructor and can set fields in your class that you declared private. Regular code can of course not do this. By applying the [Serializable] attribute, you explicitly give it the go-ahead to mess with your private parts. And you implicitly give that permission to only the BinaryFormatter class.
XML serialization doesn't need this kind of okay, it only serializes members that are public.
DataContractSerializer can serialize private members as well. It therefore needs an explicit okay again, now with the [DataContract] attribute.
First off, you don't have to.
It is simply a marker interface that tells the serializer that the class is composed of items that it can serialize (which may or may not be true) and that is can use the default serialization.
The XMLSerializer has the additional requirement to have a zero parameter constructor to the class.
There are other serializers that use contracts for serialization (such as the DataContractSerializer) - they give you more control over serialization than simply marking a class as Serializable. You can also get more control by implementing the ISerializable interface.
It's basically metadata that indicates that a class can be serialized, nothing more.
It is required by a lot of framework serializers, which refuse to deal with types not having this attribute applied to them.
Serialization can create security holes and may be plagued by versioning problems. On top of that, for some classes, the very idea of serialization is outright nonsense.
For details, see the excellent answers to Why Java needs Serializable interface?, especially this one, this one, and this one. They make the case that serialization should be a feature you have to explicitly opt into.
For a counterpoint, the accepted answer to that question makes the case that classes should be serializable by default.
It indicates to the serializer that you want that class to be serialized as you may not want all properties or classes to be serialized.
I see it as a reminder that I will allow the class to be serialized. So you don't implicitly serialize something you shouldn't.
Don't know it that is designers' intention.
BTW, I just love BinaryFormatter and use it as much as I can. It handles pretty much of the stuff automatically (like rebuilding complex object graphs with recurring references spread throughout the graph).

Serializing an object but none of its references c#

I have a situation where I need to serialize an object but don't want to serialize any of its references. This is because I don't know in advance which dlls the object might be referencing and therefore can't ensure that they are serializable objects. This has arisen from needing to serialise plugins to preserve their state.
Am I right in thinking that this is the case with XML serialization (shallow)? But that this will ignore anything private in the object - which isn't what I want?
Is this somehow possible?
Xml Serialization will only work on things that are publicly accessible. Also, unless you mark a public property / field with the [XmlIgnore] attribute, it will be serialized.
If you're just looking at some method of serialization, then use binary serialization. It will serialize the internal state of the object (all fields, private or otherwise). You can use the [NonSerialized] attribute to ignore specific references if you want.
If you know at the type declaration time which references should not be serialized you can use binary serialization and filter out members with the [NonSerialized] attribute.
Put NonSerialized attribute in case of binary and XmlIgnore attribute in case of xml serialization to reference properties or fields
You do know which properties you can serialize, though, correct? Are these plugins implementing a common interface? If that is the case, you should be able to write a generic serializer that will only serialize the specific properties that you choose.
Here is a basic example that will give you the idea of what you need to do:
Object Serialization using C#
If you are just looking to serialize native types within your class instances, you should just be able to implement ISerializable, though, and decorate the properties that you do not want to be serialized.
You can try something like this:
Type myType = currentObject.GetType();
Then check to see if the object is serializable by using:
myType.IsSerializable; //returns a bool
That should tell you whether or not the object is serializable. If you really need to know whether every single object inside of a class is serializable, such as other nested classes or custom types, then you could probably use reflection to read each object, use the code above, and verify whether or not it is serializable. This, however, might be a more complicated approach, and may not be plausible, especially if you have overhead issues to deal with.
It may be useful for you to separate what you want to persist from how you persist it.
It seems like you want control over how you want to persist data, but obviously, cannot know what it is, because of your plugin model.
One scheme that may make sense to you is to give your plugins some sort of object or interface they can write to and read from when its time to save / load. Its fine to document these constraints.
For example, when persisting, allow your plugins to pass to you:
some arbitrary byte array which they are responsible for serializing / deserializing. Then it is a plugins responsibility to make sure they use objects that are appropriately serializable.
a dictionary of strings
an xml file
others...
Store this information per plugin (in whichever form you want), and loading up again, pass back the same information.
This is simply an approach around the fact that in the end, the plugin knows what it needs to save, and needs to own that piece of information.

convert non serializable object to string in C#

I have an object (User) which is not marked as [Serializable()].
I need to convert the entire object (including child objects) to string.
This is an actual need to convert the object from a third party tool response which is not marked as [Serializable()].
How can i convert an entire C# object to string/xml of the above scenario?
The XmlSerializer does not need the Serializable attribute, but it can only serialize public members.
Best Regards
Oliver Hanappi
Edit: You can create your own adapter class, which implements the IXmlSerializable interface and represents one User object which your adapter gets when constructed.
If JSON satisfies your needs, you can try JsonExSerializer as it does not need any attributes to decorate targeted objects.
You could use reflection to find all of the members that you are interested in e.g. public properties and/or private fields and then construct an xml document as you go.
That way would could keep the code generic and as custom as you like. :)
However, remember that reflection can be a very slow process at runtime. :(

Categories