From bda33e1c8a4e9bfda7c7c995d68364420bd62de9 Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Thu, 2 May 2024 08:23:01 +0200 Subject: [PATCH] comments & fixes --- .../PluginFeatureController.cs | 108 ++++++++++++++---- 1 file changed, 84 insertions(+), 24 deletions(-) diff --git a/Pilz.Plugins.Advanced/PluginFeatureController.cs b/Pilz.Plugins.Advanced/PluginFeatureController.cs index 797f86c..1003348 100644 --- a/Pilz.Plugins.Advanced/PluginFeatureController.cs +++ b/Pilz.Plugins.Advanced/PluginFeatureController.cs @@ -7,10 +7,14 @@ using System.Threading.Tasks; namespace Pilz.Plugins.Advanced { - public sealed class PluginFeatureController + public class PluginFeatureController { + // D e l e g a t e s + public delegate void PluginFeatureEventHandler(PluginFeatureController controller, PluginFeature feature); + // S t a t i c E v e n t s + /// /// Fires when a new has been registred. /// @@ -21,11 +25,20 @@ namespace Pilz.Plugins.Advanced /// public static event PluginFeatureEventHandler? OnPluginFeatureUnregistred; + // S t a t i c M e m b e r s + + protected static readonly string nameGetFeatures = $"{nameof(IPluginFeaturesProvider.GetFeatures)}"; + protected static readonly string nameGetFeaturesExplicit = $"{typeof(IPluginFeaturesProvider).FullName}.{nameof(IPluginFeaturesProvider.GetFeatures)}"; + protected static readonly string nameInstance = $"get_{nameof(IPluginFeatureProvider.Instance)}"; + protected static readonly string nameInstnaceExplicit = $"{typeof(IPluginFeaturesProvider).FullName}.get_{nameof(IPluginFeatureProvider.Instance)}"; + /// /// The default public instance that can be used by plugins and the interface providing software. /// public static PluginFeatureController Instance { get; private set; } = new(); + // I n s t a n c e M e m e b e r s + private readonly HashSet features = []; /// @@ -50,53 +63,96 @@ namespace Pilz.Plugins.Advanced Modules = new(this); } - public void RegisterAllFromMyAssembly() + /// + /// Registers all features found in the currently executing Assembly via , and . + /// Note:
Explicit implementations of can not be detected. For this case just implement instead.
+ ///
+ public void RegisterAllOwn() { - RegisterAllFromAssembly(Assembly.GetCallingAssembly()); + RegisterAll(Assembly.GetCallingAssembly()); } - public void RegisterAllFromAssembly(Assembly assembly) + /// + /// Registers all features found in the given via , and . + /// Note:
Explicit implementations of can not be detected. For this case just implement instead.
+ ///
+ /// + public void RegisterAll(Assembly[] assemblies) { - const string nameGetFeatures = $"{nameof(IPluginFeaturesProvider.GetFeatures)}"; - const string nameGetFeaturesExplicit = $"{nameof(IPluginFeaturesProvider)}.{nameof(IPluginFeaturesProvider.GetFeatures)}"; - const string nameInstance = $"get_{nameof(IPluginFeatureProvider.Instance)}"; - const string nameInstnaceExplicit = $"get_{nameof(IPluginFeatureProvider)}.{nameof(IPluginFeatureProvider.Instance)}"; + foreach (var assembly in assemblies) + RegisterAll(assembly); + } - foreach (var type in assembly.GetTypes()) + /// + /// Registers all features found in the given via , and . + /// Note:
Explicit implementations of can not be detected. For this case just implement instead.
+ ///
+ /// + public void RegisterAll(Assembly assembly) + { + RegisterAll(assembly.GetTypes()); + } + + /// + /// Registers all features found from the given via , and . + /// Note:
Explicit implementations of can not be detected. For this case just implement instead.
+ ///
+ /// + public void RegisterAll(Type[] types) + { + foreach (var type in types) + RegisterAll(type); + } + + /// + /// Registers all features found from the given via , and . + /// Note:
Explicit implementations of can not be detected. For this case just implement instead.
+ ///
+ /// + public void RegisterAll(Type type) + { + if (type.IsAssignableTo(typeof(IPluginFeaturesProvider))) { - if (type.IsAssignableTo(typeof(IPluginFeaturesProvider))) - { - var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); - var method = methods.FirstOrDefault(n => n.Name == nameGetFeaturesExplicit || n.Name == nameGetFeatures); + var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); + var method = methods.FirstOrDefault(n => n.Name == nameGetFeaturesExplicit || n.Name == nameGetFeatures); - if (method != null && method.Invoke(null, null) is PluginFeature[] features) - { - foreach (var feature in features) - Register(feature); - } - } - else if (type.IsAssignableTo(typeof(IPluginFeatureProvider))) + if (method != null && method.Invoke(null, null) is PluginFeature[] features) { - var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); - var method = methods.FirstOrDefault(n => n.Name == nameInstnaceExplicit || n.Name == nameInstance); - - if (method != null && method.Invoke(null, null) is PluginFeature feature) + foreach (var feature in features) Register(feature); } } + else if (type.IsAssignableTo(typeof(IPluginFeatureProvider))) + { + var methods = type.GetMethods(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); + var method = methods.FirstOrDefault(n => n.Name == nameInstnaceExplicit || n.Name == nameInstance); + + if (method != null && method.Invoke(null, null) is PluginFeature feature) + Register(feature); + } } + /// + /// Registers a feature via the given or . + /// public void Register() where TProvider : IPluginFeatureProvider { Register(TProvider.Instance); } + /// + /// Registers all features via the given . + /// public void RegisterAll() where TProvider : IPluginFeaturesProvider { foreach (var feature in TProvider.GetFeatures()) Register(feature); } + /// + /// Registers the given feature. + /// + /// public void Register(PluginFeature module) { if (!features.Contains(module)) @@ -106,6 +162,10 @@ namespace Pilz.Plugins.Advanced } } + /// + /// Unregisters the given feature. + /// + /// public void Unregister(PluginFeature module) { features.Remove(module);