.netmatrixmarshalbyrefobjectdrawing2d

Why does System.Drawing.Drawing2D.Matrix derive from MarshalByRefObject?


Why is Matrix a heavyweight class derived from MarshalByRefObject instead of a lightweight struct?


Solution

  • The Matrix class is actually a wrapper to an unmanaged structure which is manipulated by the GDI+ Flat API.

    That said, it's common for the classes in the System.Drawing namespace to derive from the MarshalByRefObject class for the convenience of an IDisposable implementation as well as get automatic marshaling across the application domain boundary when used in Remoting.

    This was more than likely done because most of the GDI functions (which most of the APIs that the Windows Forms controls rely on) will use the GDI matrix for transformations; using a lightweight, fully managed code structure would require translation of that structure across the managed/unmanaged boundary every time a method was called.

    Compared to the cost of marshaling just the call and the handle, versus the call and the entire structure whenever you want to do operations on the matrix, it was probably decided that for performance reasons it was better to marshal the calls.