Here is the Unload and Load code for single assembly -- adapted for ShapeApp basic integration sample.
internal void UnloadAddIn(String name, bool skipOnShutdown)
{
System.Diagnostics.Debug.WriteLine("==================>Unload Addin: " + name + (skipOnShutdown ? " [skip OnShutdown]" : ""));
int index = 0;
for(index = 0; index < this.addInInfoList.Count; index++)
{
if (name.Equals(addInInfoList[index].token.AssemblyName.Name, StringComparison.CurrentCultureIgnoreCase))
{
if (!skipOnShutdown)
{
try
{
addInInfoList[index].entryPoint.OnShutdown();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
//Deterministic AddIn shutdown/release
AddInController controller = AddInController.GetAddInController(addInInfoList[index].entryPoint);
controller.Shutdown();
//release addin
AddInInfo ai = addInInfoList[index];
this.addInInfoList.RemoveAt(index);
ai.entryPoint = null;
ai.token = null;
break;
}
}
}
/// <summary>
/// Loads the add in.
/// </summary>
/// <param name="name">The name of the AddIn to load.</param>
/// <param name="container">The <see cref="ServiceContainer"/> to load the AddIn with.</param>
/// <returns><c>true</c> if the AddIn is loaded successfully; <c>false</c> otherwise.</returns>
internal bool LoadAddIn(String name, AddInProcess addInProcess)
{
//The AddIn should be in a .dll file having the same short
//name as the directory it's in.
String dllPath = System.IO.Path.Combine(System.IO.Path.Combine(CorelApplicationIntegration.AddInPath, name), name + ".dll");
//If the directory does not contain that file, skip it.
if (!System.IO.File.Exists(dllPath))
return false;
//Create the AddInToken.
Collection<AddInToken> addinToken = AddInStore.FindAddIn(typeof(IEntryPoint), AddInStoreExtensions.DefaultPipelinePath, dllPath, name + "." + StartUpClass);
//Assert the AddIn is valid.
System.Diagnostics.Debug.Assert(addinToken.Count >= 1);
//If the AddIn is not valid return false.
if (addinToken.Count == 0)
return false;
// Using the Token returned above, load the AddIn. This will create an
// instance of AddIn EntryPoint class to activate the AddIn.
IEntryPoint addIn;
if (addInProcess == null)
{
System.Diagnostics.Debug.WriteLine("LoadAddIn In-Process");
// Load the AddIn in the same process as the host.
addIn = addinToken[0].Activate<IEntryPoint>(AddInSecurityLevel.FullTrust);
}
else
{
System.Diagnostics.Debug.WriteLine("==================>LoadAddIn Ex-Process<=================");
// Load the AddIn in external process.
addIn = addinToken[0].Activate<IEntryPoint>(addInProcess, AddInSecurityLevel.FullTrust);
}
// Initialize AddIn by calling into functions of the AddIn EntryPoint instance.
addIn.Initialize(this.serviceContainer);
addIn.InitializeDataBindings();
addIn.FinishInitialization();
//Store that AddIn to stop it later.
AddInInfo ai = new AddInInfo(addIn, addinToken[0]);
this.addInInfoList.Add(ai);
//The AddIn was loaded so return true.
return true;
}
public struct AddInInfo
{
public IEntryPoint entryPoint;
public AddInToken token;
public AddInInfo(IEntryPoint entryPt, AddInToken tok)
{
entryPoint = entryPt;
token = tok;
}
}
private List<AddInInfo> addInInfoList;