C# 电话簿项目的通用方法

我正在尝试制作电话簿项目,其中我从 bin 文件写入/读取数据,我在域类库中有两个类,用户和联系人,现在我想在 FileManager 类中创建私有通用函数,添加/编辑/删除和获取它将为联系人和用户找到/工作,

我如何知道private T Get<T>(int id) where T : class函数中给出的是哪种类型?使其适用于两种类型

如何正确完成这些功能呢?


慕运维8079593
浏览 32回答 1
1回答

繁星点点滴滴

我认为您应该分别为 User 和 Contact 类创建一个通用接口及其实现。如果出现一个新类,例如 Employee - 您将对此接口进行新的实现,而无需对 User 和 Contact 类进行任何更改。如果源不是二进制文件,而是数据库 - 那么该接口的单独实现。如下:interface IManager<TEntity> where TEntity : class&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; IList<TEntity> GetAll();&nbsp; &nbsp; &nbsp; &nbsp; TEntity GetById(int id);&nbsp; &nbsp; &nbsp; &nbsp; void Add(TEntity entity);&nbsp; &nbsp; &nbsp; &nbsp; void Update(TEntity entity);&nbsp; &nbsp; &nbsp; &nbsp; void Remove(int id);&nbsp; &nbsp; &nbsp; &nbsp; int GenerateContactId();&nbsp; &nbsp; &nbsp; &nbsp; IList<TEntity> Search(Func<TEntity, bool> p);&nbsp; &nbsp; }&nbsp; &nbsp; class BinaryContactManager : IManager<Contact>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public void Add(Contact entity)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NotImplementedException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public int GenerateContactId()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NotImplementedException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public IList<Contact> GetAll()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NotImplementedException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public Contact GetById(int id)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NotImplementedException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void Remove(int id)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NotImplementedException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public IList<Contact> Search(Func<Contact, bool> p)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NotImplementedException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void Update(Contact entity)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NotImplementedException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; class BinaryUserManager : IManager<User>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public void Add(User entity)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NotImplementedException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public int GenerateContactId()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NotImplementedException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public IList<User> GetAll()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NotImplementedException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public User GetById(int id)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NotImplementedException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void Remove(int id)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NotImplementedException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public IList<User> Search(Func<User, bool> p)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NotImplementedException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public void Update(User entity)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new NotImplementedException();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP