亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C# 電話簿項目的通用方法

C# 電話簿項目的通用方法

PHP
慕運維8079593 2024-01-20 16:14:08
我正在嘗試制作電話簿項目,其中我從 bin 文件寫入/讀取數據,我在域類庫中有兩個類,用戶和聯系人,現在我想在 FileManager 類中創建私有通用函數,添加/編輯/刪除和獲取它將為聯系人和用戶找到/工作,我如何知道private T Get<T>(int id) where T : class函數中給出的是哪種類型?使其適用于兩種類型如何正確完成這些功能呢?
查看完整描述

1 回答

?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

我認為您應該分別為 User 和 Contact 類創建一個通用接口及其實現。如果出現一個新類,例如 Employee - 您將對此接口進行新的實現,而無需對 User 和 Contact 類進行任何更改。如果源不是二進制文件,而是數據庫 - 那么該接口的單獨實現。


如下:


interface IManager<TEntity> where TEntity : class

    {

        IList<TEntity> GetAll();

        TEntity GetById(int id);

        void Add(TEntity entity);

        void Update(TEntity entity);

        void Remove(int id);

        int GenerateContactId();

        IList<TEntity> Search(Func<TEntity, bool> p);

    }


    class BinaryContactManager : IManager<Contact>

    {

        public void Add(Contact entity)

        {

            throw new NotImplementedException();

        }


        public int GenerateContactId()

        {

            throw new NotImplementedException();

        }


        public IList<Contact> GetAll()

        {

            throw new NotImplementedException();

        }


        public Contact GetById(int id)

        {

            throw new NotImplementedException();

        }


        public void Remove(int id)

        {

            throw new NotImplementedException();

        }


        public IList<Contact> Search(Func<Contact, bool> p)

        {

            throw new NotImplementedException();

        }


        public void Update(Contact entity)

        {

            throw new NotImplementedException();

        }

    }


    class BinaryUserManager : IManager<User>

    {

        public void Add(User entity)

        {

            throw new NotImplementedException();

        }


        public int GenerateContactId()

        {

            throw new NotImplementedException();

        }


        public IList<User> GetAll()

        {

            throw new NotImplementedException();

        }


        public User GetById(int id)

        {

            throw new NotImplementedException();

        }


        public void Remove(int id)

        {

            throw new NotImplementedException();

        }


        public IList<User> Search(Func<User, bool> p)

        {

            throw new NotImplementedException();

        }


        public void Update(User entity)

        {

            throw new NotImplementedException();

        }

    }



查看完整回答
反對 回復 2024-01-20
  • 1 回答
  • 0 關注
  • 124 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號