我想用一個代碼行生成正確的對象,而不是開關的情況下,因爲總是添加新設備時,我必須添加一個新行。對象反射c#
是否有可能在一行沒有開關的情況下做到這一點?
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的使用呢? –