열거 형을 사용하는 데 문제가 있습니다. 내 장치 컨테이너에서 사용할 장치를 지정하기 위해 외부 클라이언트가 사용하는 DeviceType의 이름을 지정하는 enum을 정의했다고 가정 해 보겠습니다. 하지만 enum은 확장 할 수 없기 때문에 라이브러리를 업데이트하지 않고 모든 사용자가 새 버전으로 업데이트하지 않고도 새 장치를 가질 수는 없습니다. 가능한 한이 문제에 대한 간단한 해결책을 찾고 있습니다. 저는 속성이나 다른 .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;
}
}
}
?????????????? –
그냥 열거 형 대신'사전'을 사용할 수 있습니까? –
기본적으로 "열거 형을 사용하고 싶지만 열거 형은 내가 원하는 것을 할 수 없다"는 말처럼 들립니다. 열거 형을 사용하지 않는 것이 좋습니다. – bengoesboom