【发布时间】:2018-08-31 15:43:19
【问题描述】:
我有一个代表工厂传感器的类。由于历史原因,相似的对象(因此由同一类表示)具有不同的标识:
- 有些有名字(即“north-west-gummy-bear”)
- 有些有 areaId 和 sensorId
为了适应这一点,我使用了一个空接口:
public class sensor
{
ISensorIdentifier id{get;set;}
}
public interface ISensorIdentifier{
}
public class namedSensorID:ISensorIdentifier{
string name{get;set;}
}
public class idSensorID:ISensorIdentifier{
int areaID{get;set;}
int sensorID{get;set;}
}
这允许我对具有不同识别系统的对象使用相同的类。
据我了解,空接口是一种代码异味,我应该改用自定义属性。
但是,在阅读了自定义属性之后,我不知道从哪里开始。确实,我可以使用自定义属性 'sensorIdentifier' 而不是空接口,但我应该如何在 sensor 类中键入 id 属性?
【问题讨论】:
标签: c# interface attributes