我想用一个代码行生成正确的对象,而不是开关的情况下,因为总是添加新设备时,我必须添加一个新行。对象反射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的使用呢? –