Object Detection Empty (Not Null)

I am sure this is a very useful thing, but I can not find the information ...

I have an Obejct series serializer and deserialization that occurs in my program. Objects have fields with a null value, one of which is the DefaultValue field and is a reference to the object.

When this object reference is null before serialization, the deserialized object contains a reference to an empty object.

What is the best way to detect this empty object? Comparison with null termination, because it refers to an empty System.Object, as well as comparison with a new object.

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

 }
}
+3
source share
1 answer

May be...

if ((deserialisedObj.DefaultValue != null)
    && (deserialisedObj.DefaultValue.GetType() != typeof(object)))
{
    // ...
}
+1
source

Source: https://habr.com/ru/post/1767881/


All Articles