I’d like to create a plug-in architecture where I can limit an assemblies API to something very restricted, i.e. only allow a whitelist of functions.
Is it possible to restrict what functions/methods a plug in assembly can call?
Can I do it using AppDomains?
Does anyone have a simple example?
.NET has added the “Managed Addin Framework” that might fit the bill. It has the following features:
Most approaches to isolation also limit communication and UI integration. MAF attempts to get around those limitations. It requires that you setup contractual communication pipelines, but will perform most of the work you would normally have to do yourself.
An example would be stitching together UI pieces running in two seperate processes (this is magic) or being able to raise events across an AppDomain or process. These things are non-trivial, but MAF helps a lot in this regard.
Sample
Here’s a simple example. As the “Shell” author, you’ll be supplying a contract to your plugin authors. Here’s a typical contract (it’s just an abstract class):
If a plugin author wanted to write a plugin, they would simply subclass this contract and add the “Addin” attribute:
And here’s how you would load these addins and interact with them:
That’s pretty much the gist. There’s obviously more to it than that, but you get the idea.
Further Reading
Good intro article
https://web-beta.archive.org/web/20140820145919/http://msdn.microsoft.com/en-us/magazine/cc163476.aspx
Overview on MSDN
http://msdn.microsoft.com/en-us/library/bb384200.aspx
System.Addin on Codeplex (lots of samples)
http://www.codeplex.com/clraddins
Tools
Pipeline Builder (helps to generate communication pipeline between shell and addins)
http://clraddins.codeplex.com/wikipage?title=Pipeline%20Builder&referringTitle=Home
Fx-Cop rules for System.Addin
http://clraddins.codeplex.com/wikipage?title=Add-in%20FxCop%20Rules&referringTitle=Home