I have two classes named Parent and Child. The Parent class contains a reference to the Child class. When I create an instance of the Parent class I will pass an instance of a Child object.
As per my knowledge we pass this Child instance as reference. Then I cache this Parent object in an external cache. Then I retrieve this object from another system. I think in that system the instance of the Child object may not be present.
As per theory ‘_child’ contains reference of object in the heap not the actual object am i right?
Just think in server farm scenario, your object instance should be available to other system and the system once generated that object
public class Parent
{
public Child _child = null;
public Parent(Child c)
{
_child=c;
}
}
public class Child
{
public string Name;
}
static void Main()
{
Child c = new Child();
c.Name="vivek";
Parent p = Parent(c);
string key = ExternalCache.Add(p);
}
Now what will happen if I try to retrieve this Parent object in another system or application?
static void Main()
{
Parent p = (Parent)ExternalCache.Get(Key)
Console.WriteLine(p._child.Name);
}
It’s not possible to pass the same references of objects to different machines simply because they won’t be running within the same memory space so even if you could the references wouldn’t be any use. However, what you can do is serialize the object graph & re-contruct the object at the other end – this is known as object serialization, or just serialization in general.
The .NET Framework has various built-in serializers which allow different types of serialization e.g.