新しいオブジェクトが追加されたときに常に新しい行を追加する必要があるため、スイッチオブジェクトではなく1つのコード行で正しいオブジェクトを生成します。オブジェクトのリフレクションc#
スイッチケースを使用せずに1行で行うことはできますか?
public static Device GetDevice(Device.enumDevice TypeOfDevice, string alias)
{
// Create the Object with using reflection
switch (TypeOfDevice)
{
case Device.enumDevice.A34411:
return new A34411(string alias);
break;
case Device.enumDevice.N5744:
return new N5744(string alias);
break;
default:
throw new NotImplementedException();
}
return null;
}
反射はどこですか? – oerkelens
クラスにカスタム属性を置き、リフレクションを使用して、 'Device'ベースクラスのすべてのサブクラスを列挙し、その属性を正しい値で探します。 –
あなたの計画もenumの使用を削除することですか? –