您可以在 的实现上使用属性Engine2,如下所示:
[AttributeUsage(AttributeTargets.Class)]
public class HandlesGridAttribute : Attribute { }
然后您将其应用于您的推导:
[HandlesGrid]
public Engine2 : EngineBase { ... }
然后,在您的客户端中,检查属性:
IEnumerable<EngineBase> bases = ...;
// Get all the implementations which handle the grid.
IEnumerable<EngineBase> handlesGrid = bases.
Where(b => b.GetType().
GetCustomAttributes(typeof(HandlesGridAttribute), true).Any());
// Set the active property.
foreach (EngineBase b in handlesGrid) b.Active = true;
这里的主要缺点(可能适用于您,也可能不适用于您)是您无法在运行时更改值(因为该属性是在编译时烘焙的)。如果您的引擎以这种方式不是动态的,那么属性是正确的方法。
如果您需要更改派生是否可以在运行时执行此操作,那么您必须退回到您的第二个选项,即识别引擎属性的代码结构。请注意,它不一定是字符串(我也不喜欢那样),但它可以是更有条理的东西,可以为您提供您正在寻找的信息。