add generic Get and GetAll methods

This commit is contained in:
Pilzinsel64
2025-07-02 07:42:23 +02:00
parent bc590923ad
commit 02fd8246cd
3 changed files with 27 additions and 4 deletions

View File

@@ -0,0 +1,5 @@
namespace Pilz.Plugins.Advanced.Exceptions;
public class PluginFeatureException : Exception;
public class PluginFeatureNotFoundException : PluginFeatureException;

View File

@@ -8,7 +8,7 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<Version>2.10.6</Version> <Version>2.10.7</Version>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

View File

@@ -1,4 +1,5 @@
using System.Reflection; using Pilz.Plugins.Advanced.Exceptions;
using System.Reflection;
namespace Pilz.Plugins.Advanced; namespace Pilz.Plugins.Advanced;
@@ -193,7 +194,24 @@ public class PluginFeatureController
public virtual IEnumerable<PluginFeature> GetAll() public virtual IEnumerable<PluginFeature> GetAll()
{ {
return controller.features.ToArray(); return [.. controller.features];
}
public virtual IEnumerable<T> GetAll<T>() where T : PluginFeature
{
return controller.features.OfType<T>();
}
public virtual T? Get<T>() where T : PluginFeature
{
return GetAll<T>().FirstOrDefault();
}
public virtual T? Get<T>(bool throwIfNull) where T : PluginFeature
{
if (Get<T>() is T feature)
return feature;
throw new PluginFeatureNotFoundException();
} }
public virtual IEnumerable<PluginFeature> Get(string featureType) public virtual IEnumerable<PluginFeature> Get(string featureType)
@@ -221,7 +239,7 @@ public class PluginFeatureController
{ {
public override IEnumerable<T> GetAll() public override IEnumerable<T> GetAll()
{ {
return controller.features.OfType<T>(); return GetAll<T>();
} }
public override IEnumerable<T> Get(string moduleType) public override IEnumerable<T> Get(string moduleType)