列挙型の使用に問題があります。外部のクライアントが使用する DeviceType の名前を示す enum を定義して、Devices コンテナーから使用するデバイスを指定したとします。しかし、列挙型は拡張できないため、ライブラリを更新してすべてのユーザーを新しいバージョンに更新しないと、新しいデバイスを使用できません。この問題のできるだけ簡単な解決策を探しています。属性やその他の .NET の「ごまかし」グッズは使用したくありません。
public class Program
{
private static List<IDevice> devices;
public static void Main(String[] args)
{
devices = new List<IDevice>()
{
new NetworkDevice()
};
IEnumerable<IDevice> currentDevices = GetDevices(DeviceType.Network);
IEnumerable<IDevice> newDevices = GetDevices(DeviceType.NewNetwork); // Will not work, unless client updates my library to get newly added enum types
}
private static IEnumerable<IDevice> GetDevices(DeviceType type)
{
return devices.Where(device => device.Type == type);
}
}
public enum DeviceType
{
Network
}
public interface IDevice
{
DeviceType Type { get; }
}
public class NetworkDevice : IDevice
{
public DeviceType Type
{
get
{
return DeviceType.Network;
}
}
}