Just in case readers are curious about what a lambda expression looks like for an event callback, here is some code snippet:
public void Initialize()
{
//TODO: add code to run when the ExtApp initializes. Here are a few examples:
// Checking some host information like build #, a patch or a particular Arx/Dbx/Dll;
// Creating/Opening some files to use in the whole life of the assembly, e.g. logs;
// Adding some ribbon tabs, panels, and/or buttons, when necessary;
// Loading some dependents explicitly which are not taken care of automatically;
// Subscribing to some events which are important for the whole session;
// Etc.
Editor editor = MgdAcApplication.DocumentManager.MdiActiveDocument.Editor;
editor.PointMonitor += (sender,e) =>
{
Editor ed = sender as Editor; //Wrong Use: (Editor)sender;
if (ed == null) return;
if ((e.Context.History & PointHistoryBits.LastPoint) == PointHistoryBits.LastPoint) return;
bool snapped = (e.Context.History & PointHistoryBits.ObjectSnapped) == PointHistoryBits.ObjectSnapped;
Point3d pt = (snapped ? e.Context.ObjectSnappedPoint : e.Context.ComputedPoint).TransformBy(ed.CurrentUserCoordinateSystem.Inverse());
ed.WriteMessage("{0}: {1:F4}\n", snapped ? "Snapped" : "Found", pt);
};
//mEditorEvents1.Register();
}
As can be seen, it's nothing more than moving the PointMonitor event callback function to be inside (or inline) the caller. Though it seems saving the callback definition name, it brings about quite some issues, as discussed previously. Even for the lambda itself, it may cause quite some confusions as follows:
- The 'return' code line would return to where?
- How to handle exceptions for the PointMonitor and other logic?
- What about the owner of the PointMonitor is named also as 'ed'?
- What if the code needs to be called inside another event callback (which is very common)?
- ...
That explains why our software such as AutoCAD .NET Addin Wizard does not use the lambda expressions in any event handlers. It is not we don't know or like lambda expressions. It's simple like that not proper in the circumstance. As demonstrated previously, all the event callbacks, the event registration and unregistration, and the hookup into the Extension Application are all structured and handled well there.
The latest build of AutoCAD .NET Addin Wizard 2015 can be downloaded and purchased from the following page:
Recent Comments