views:

67

answers:

3

I have a plugin interface that allows third-party developers to add custom plugins into my application.

Performance and application responsiveness is very important to me, so I provide explicit "Initialize" and "Shutdown" implementations in my interface contracts. However, one thing out of my control is constructor code that should not be used, and could block my thread.

What's the best way to detect whether or not a third party developer is using code in a class's constructor, so that I can enforce this design decision?

Update: Reason for this is similar to a desktop widgets concept. You want to load the complete picture (i.e. get all the widgets on screen as fast as possible), and then enable the widget processing, start background threads, etc. Obviously we don't want to start unnecessary things in the constructor of a UI control.

Not necessarily looking to decompile the IL of the third party class, as much as find the most simple and direct way of detecting unnecessary work happening. Timing is one, but wondering if there would be anything left behind in the stack trace, or other creative ways of determining this information.

+1  A: 

The best, and only way, is to inspect the underlying IL of the constructor, and all of the base class constructors to ensure they only use the code which is allowed in the constructor. No code is not a reasonable option as it would prevent both field assignment and base class constructor chaining.

If the constructor issue is really that important then why even create the object on your main thread. Why not instead spawn off another thread to do the ctor work and abandon the thread if the constructor takes too long? This is both a more flexible solution as it allows good customers to write good code and prevents you from having to learn the arcane art of translating a reflection produced byte[] into IL op codes

JaredPar
A: 

You can easily find the constructors on a type using reflection via Type.GetConstructors. determining whether or not there is offending code is another thing entirely.

Sky Sanders
+1  A: 

The only way you can enforce it is to provide a base class that all plugins have to inherit from. You'll control the calls and can make your constructor private. Then create factory methods as part of your interface.

But tell me - how do you keep them from making blocking calls in initialize? Why can't you use the same mechanism when creating an object?

No Refunds No Returns
I like the inheritance idea.. If they make blocking calls in Initialize/Shutdown, that's fine, because I can control when it happens in the application lifecycle as to not disrupt the user experience...
routeNpingme
About the only down side is that you limit plug-in writers to what they can use as a starting point.
No Refunds No Returns