我有一種情況,我映射具有繼承層次結構的類。我有一個每個班級都知道它需要的項目清單。例如:什麼是從C#中的子類中聚合屬性的優雅方法?
public class PersonMapper
{
private string[] attributes = new[] {"firstName", "lastName"};
}
public class UserMapper : PersonMapper
{
private string[] attributes = new[] {"username"};
}
public class EmployeeMapper : PersonMapper
{
private string[] attributes = new[] {"employeeId"};
}
我想就返回所有的超類的這樣的聚合方法調用:
EmployeeMapper mapper = new EmployeeMapper();
string[] attributes = mapper.GetAttributes();
Assert.That(attributes, Has.Member("firstname"));
我能想到的一些解決方案,但他們似乎過於複雜。我覺得我缺少一個優雅的解決方案。誰能幫忙?
在此先感謝!
接受4分鐘內。大討論男孩,謝謝! – 2009-02-27 17:43:38