C#: How to remove all null properties from a generic object using reflection? -
I want to remove all the empty properties in a normal object. It is not necessary to be recursive, a level deep too is also fine.
I have a custom javascript controller implementation that implements JSON serialization which gives me: {"name": "alim", "age": zero, "type": "employee "}
And I would like to leave the empty object.
Takes objct to work for this work and a dictionary:
IDictionary & lt; String, Object & gt; Serialize (object obj, JavaScriptSerializer serializer)
So I would like to remove all the blank properties from obj but all the properties get pleasure but if the property is not set, gator returns null .
You can apply yourself to handle your type of ordering. Then you get full control of how the properties are serialized.
Provides a good implementation of @Rickshade's answer serialization method.
The Deserialize method will be exactly the same, but I am implementing you now, the only drawback with the JavaScriptConverter is that it has to get the supported type from somewhere. Either this hard code likes it:
Public override IEnumerable & lt; Type & gt; Supported type {{var list = new list & lt; Type & gt; {Typeof (Foo), typef (bar) ...}; Return list }} ... or make it configurable, e.g. Through Class Constructor
Comments
Post a Comment