[Mono-list] OnDeserialization problem

Chris Howie cdhowie at gmail.com
Thu Apr 9 20:55:19 EDT 2009


On Mon, Mar 23, 2009 at 12:35 PM, Dave Null <eric.jacoboni at gmail.com> wrote:
> BTW... What is the preferred way to achieve this kind of stuff ?
>
> 1) using IDeserializationCallback interface and implementing
> OnDeserialization() method, as i've done above
> 2) or, simply using [OnDeserializated] attribute as in:

I prefer the first method since using the attribute requires (IIRC) a
public method, which is unsightly and can "poison" your API with
superfluous methods.

If you use the interface you can implement the method explicitly (you
do so implicitly, so it still show up public).  This does not prevent
other developers from using that method, but it does require them to
first cast to the interface type.

If you want to allow this method to be overridden (you seem to based
on the virtual keyword) then you can do something like this:

class Foo : IDeserializationCallback {
    void IDeserializationCallback.OnDeserialization(object sender) {
        OnDeserialization(sender);
    }

    protected virtual void OnDeserialization(object sender) {
    }
}

This gets all of the deserialization-related methods out of the public
API, while still allowing derived classes to override the
deserialization hook.

-- 
Chris Howie
http://www.chrishowie.com
http://en.wikipedia.org/wiki/User:Crazycomputers


More information about the Mono-list mailing list