/// <summary> /// Register the <typeparamref name="T1"/> as plugin. This can be an interface, an abstract class or /// a usual class which is a base class. /// </summary> /// <typeparam name="T1">This can be an interface, an abstract class or a usual class which is a base class.</typeparam> /// <param name="myMinVersion">The minimum allowed version.</param> /// <param name="myMaxVersion">The maximum allowed version. If null all version greater than <paramref name="myMinVersion"/> are valid.</param> /// <param name="myActivateDelegate">Using this delegate you can activate the type instance.</param> /// <param name="myCtorArgs">Optional constructor parameters which will be used at the activation time.</param> /// <returns>The same instance to register more types in a fluent way.</returns> public PluginManager Register <T1>(Version myMinVersion, Version myMaxVersion = null, Func <Type, Object> myActivateDelegate = null, params Object[] myCtorArgs) { _inheritTypeAndInstance.Remove(typeof(T1)); var activatorInfo = new ActivatorInfo { Type = typeof(T1), MinVersion = myMinVersion, MaxVersion = myMaxVersion, CtorArgs = myCtorArgs, ActivateDelegate = myActivateDelegate }; _inheritTypeAndInstance.Add(typeof(T1), new InstanceContainer { Info = activatorInfo, Objects = new List <object>() }); return(this); }
/// <summary> /// Register the <typeparamref name="T1"/> as plugin. This can be an interface, an abstract class or /// a usual class which is a base class. /// </summary> /// <typeparam name="T1">This can be an interface, an abstract class or a usual class which is a base class.</typeparam> /// <param name="myMinVersion">The minimum allowed version.</param> /// <param name="myMaxVersion">The maximum allowed version. If null all version greater than <paramref name="myMinVersion"/> are valid.</param> /// <param name="myActivateDelegate">Using this delegate you can activate the type instance.</param> /// <param name="myCtorArgs">Optional constructor parameters which will be used at the activation time.</param> /// <returns>The same instance to register more types in a fluent way.</returns> public PluginManager Register <T1>(Version myMinVersion, Version myMaxVersion = null, Func <Type, Object> myActivateDelegate = null, params Object[] myCtorArgs) { if (_inheritTypeAndInstance.ContainsKey(typeof(T1))) { throw new Exception("Duplicate activator type '" + typeof(T1).Name + "'"); } var activatorInfo = new ActivatorInfo { Type = typeof(T1), MinVersion = myMinVersion, MaxVersion = myMaxVersion, CtorArgs = myCtorArgs, ActivateDelegate = myActivateDelegate }; _inheritTypeAndInstance.Add(typeof(T1), new Tuple <ActivatorInfo, List <object> >(activatorInfo, new List <object>())); return(this); }
private Boolean CheckVersion(bool myThrowExceptionOnIncompatibleVersion, Assembly myPluginAssembly, AssemblyName myBaseTypeAssembly, AssemblyName myPluginReferencedAssembly, ActivatorInfo myActivatorInfo) { Boolean _validVersion = false; #region Check version //if (myBaseTypeAssembly.Version != myPluginReferencedAssembly.Version) //Console.WriteLine("Assembly version does not match! Expected '{0}' but current is '{1}'", myLoadedPluginAssembly.GetName().Version, pluginReferencedAssembly.Version); if (myActivatorInfo.MaxVersion != null) { #region Compare min and max version if (myPluginReferencedAssembly.Version.CompareTo(myActivatorInfo.MinVersion) < 0 || myPluginReferencedAssembly.Version.CompareTo(myActivatorInfo.MaxVersion) > 0) { _validVersion = false; if (OnPluginIncompatibleVersion != null) { OnPluginIncompatibleVersion(this, new PluginIncompatibleVersionEventArgs(myPluginAssembly, myPluginReferencedAssembly .Version, myActivatorInfo. MinVersion, myActivatorInfo. MaxVersion, myActivatorInfo.Type)); } if (myThrowExceptionOnIncompatibleVersion) { throw new IncompatiblePluginVersionException(myPluginAssembly, myPluginReferencedAssembly.Version, myActivatorInfo.MinVersion, myActivatorInfo.MaxVersion); } } else { _validVersion = true; } #endregion } else { #region Compare min version if (myPluginReferencedAssembly.Version.CompareTo(myActivatorInfo.MinVersion) < 0) { _validVersion = false; if (OnPluginIncompatibleVersion != null) { OnPluginIncompatibleVersion(this, new PluginIncompatibleVersionEventArgs(myPluginAssembly, myPluginReferencedAssembly .Version, myActivatorInfo. MinVersion, myActivatorInfo. MaxVersion, myActivatorInfo.Type)); } if (myThrowExceptionOnIncompatibleVersion) { throw new IncompatiblePluginVersionException(myPluginAssembly, myPluginReferencedAssembly.Version, myActivatorInfo.MinVersion); } } else { _validVersion = true; } #endregion } #endregion return(_validVersion); }
/// <summary> /// Will seach all registered type whether it is an plugin definition of <paramref name="myCurrentPluginType"/>. /// </summary> /// <param name="myThrowExceptionOnIncompatibleVersion">Truth value of throw an exception</param> /// <param name="myLoadedPluginAssembly">The assembly from which the <paramref name="myCurrentPluginType"/> comes from.</param> /// <param name="myCurrentPluginType">The current plugin (or not).</param> private void FindAndActivateTypes(bool myThrowExceptionOnIncompatibleVersion, Assembly myLoadedPluginAssembly, Type myCurrentPluginType) { IEnumerable <KeyValuePair <Type, InstanceContainer> > validBaseTypes = _inheritTypeAndInstance.Where(kv => { Type realType = DeGenerification(kv.Key, myCurrentPluginType); return(kv.Key.IsBaseType(realType) || kv.Key.IsInterfaceOf(realType)); } ); #region Take each baseType which is valid (either base or interface) and verify version and add foreach (var baseType in validBaseTypes) { ActivatorInfo activatorInfo = _inheritTypeAndInstance[baseType.Key].Info; #region Get baseTypeAssembly and plugin referenced assembly AssemblyName baseTypeAssembly = Assembly.GetAssembly(baseType.Key).GetName(); AssemblyName pluginReferencedAssembly = myLoadedPluginAssembly.GetReferencedAssembly(baseTypeAssembly.Name); #endregion Boolean _validVersion = false; try { if (CheckVersion(myThrowExceptionOnIncompatibleVersion, myLoadedPluginAssembly, baseTypeAssembly, pluginReferencedAssembly, activatorInfo)) { _validVersion = true; } } catch (Exception) { continue; } #region Create instance and add to lookup dict if (_validVersion) { try { Object instance; Type realType = DeGenerification(baseType.Key, myCurrentPluginType); if (activatorInfo.ActivateDelegate != null) { instance = activatorInfo.ActivateDelegate(realType); } else { instance = Activator.CreateInstance(realType, activatorInfo.CtorArgs); } if (instance != null) { _inheritTypeAndInstance[baseType.Key].Objects.Add(instance); if (OnPluginFound != null) { OnPluginFound(this, new PluginFoundEventArgs(myCurrentPluginType, instance)); } } } catch (Exception e) { throw new UnknownException(e); } } #endregion } #endregion }