tags:

views:

62

answers:

1

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();
  }

 }
}
+1  A: 

Maybe...

if ((deserialisedObj.DefaultValue != null)
    && (deserialisedObj.DefaultValue.GetType() != typeof(object)))
{
    // ...
}
LukeH
Will test his out and see if it solves my problem.
Matt Fellows
Yes this looks like it will work... I was concerned about comparisons when the DefaultValue is an int (Int32) - but I think this will be fine?
Matt Fellows