Reputation: 6532
I'm sure this is a very noob thing to ask but I can't seem to find the information...
I've got some Obejct serialisatino and deserialisation that occurs in my program. The objects have nullable fields one of which is a field called DefaultValue and is an object reference.
When this object reference is null before serialisation, the deserialised object contains a reference to an empty object.
What is the best way to detect this empty object? Comparisons to null fail as it is referencing an empty System.Object, as do comparisons to a new Object.
Some pseudocode to highlight my problem....
class MyObj
{
public object DefaultValue {get; set;}
public object AnotherValue {get; set;}
}
class Program
{
internal static void Main()
{
MyObj obj = new MyObj();
obj.AnotherValue = "Some String";
//Serialse object
String serialisedObject = Serialise(obj);
//Deserialse object
MyObj deserialisedObj = Deserialise(serialisedObject);
if (deserialisedObj.DefaultValue != null) //This will always be true :(
{
String default = deserialisedObj.DefaultValue.ToString();
}
}
}
Upvotes: 3
Views: 783
Reputation: 269628
Maybe...
if ((deserialisedObj.DefaultValue != null)
&& (deserialisedObj.DefaultValue.GetType() != typeof(object)))
{
// ...
}
Upvotes: 1