【发布时间】:2011-08-27 17:30:43
【问题描述】:
如果不将对象序列化为字节数组,这可能是不可能的,但我想要一个对象集合,其中唯一的公共属性将是 int 和 bool:
public class Speakers : IStereoComponents
{
public int Id { get; set; }
public bool RemoveMe { get; set; }
public string SomethingElse { get; set; }
}
public class Receiver : IStereoComponents
{
public int Id { get; set; }
public bool RemoveMe { get; set; }
public IList<string> Buttons { get; set; }
}
public interface IStereoComponents
{
int Id { get; set; }
bool RemoveMe { get; set; }
}
我想要做的是能够将这些项目添加到列表或字典中,并根据 Id 和 RemoveMe 字段删除或添加它们。我希望能够做类似的事情:
foreach(组件中的var组件)component.RemoveMe == true;
有没有简单的方法来做到这一点?我能想到的唯一方法是使用反射,将所有内容序列化为一个字节 [],除了 Id 和 RemoveMe 属性。这将是一件苦差事,我希望有一个花哨的 C# 4.0 来做这件事?还是我完全遗漏了什么?
【问题讨论】: