Monday, July 02, 2007

IObjectReference interface.

Recently during my project i came across IObjectReference interface. We had a situation where we were serializing and deserializing an object. During this process, the object returned is the one which the serialized stream specifies. Here comes in the IObjectReference to save us!!!!It returns the real object that should be deserialized, rather than the object that the serialized stream specifies.

Here's an example showing how it can be used:

[Serializable]
class S : IObjectReference
{
int i;
static S S5 = new S(5);
S(int i) { this.i = i; }
public static S Get(int i) { return (i == 5) ? S5 : new S(i); }


#region IObjectReference Members
public object GetRealObject(StreamingContext context)
{
return Get(i);
}
#endregion
}

No comments:

Post a Comment