从母类实现非通用 IEnumerable

问题描述

我想就我面临的问题向专家咨询。在自下而上的方法中,我有一个类 StandardAccount,用几个简单的属性初始化,其中一个是 enum(和一个在实例化时自动设置的 GUID,如

public class StandardAccount
{
    private Guid _id;
    private string _accName;
    private AccountType _accType;
    private double _balance = 0;
    public enum AccountType
    {
        [Description("Asset")]
        AT,[Description("Liability")]
        LY,[Description("Profit")]
        PT,[Description("Loss")]
        LS
    }

    public StandardAccount(string name,AccountType type)
    {
        this._id = Guid.NewGuid();
        this._accName = name;
        this._accType = type;
        this.Balance = 0;
    }
    public double Balance { get => _balance; set => _balance = value; }
}

Book 类必须有一个或多个这些 StandardAccounts 的列表,而 Accounting 类必须有许多 Books。我以一种可搜索的方式准备我的 Book 类(将有许多 StandardAccount 列表,我需要能够稍后在这些列表中通过 GUID 找到 StandardAccount)。我按如下方式设置我的 Book 类:

public class Book
{
    private string _bookName;
    private short _bookNum;
    private Guid _bookId;

    public List<StandardAccount> Accounts { get; set; }
    public IEnumerator<StandardAccount> GetEnumerator() => Accounts.GetEnumerator();

    //compile time err: containing type does not implement interface 'IEnumerable'
    IEnumerator IEnumerable.GetEnumerator() => Accounts.GetEnumerator();
    
    //updated
    public void Add(string name,StandardAccount.AccountType type) => Accounts.Add(new StandardAccount(name,type));

    public Book(Guid? id,short BookNumber,string BookName,IEnumerable<StandardAccount> Account)
    {
        this._bookId = id ?? Guid.NewGuid();
        this._bookNum = BookNumber;
        this._bookName = BookName;
        this.Accounts = new List<StandardAccount>();
    }
    public Guid id { get => _bookId; set => _bookId = value; }
}

我有这两个错误使我无法继续前进,我不理解它们(因为我试图在“母亲”Book 类中而不是在 StandardAccount 中实现枚举器) 有人可以帮忙和建议吗?

注意:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;

我正在尝试这样做:

//this works fine
StandardAccount stdAccount = new StandardAccount("account one",StandardAccount.AccountType.AT);
//this workds fine
stdAccount.Balance = 123;

//but.. cannot add the account to my book
//ERROR - System.NullReferenceException: 'Object reference not set to an instance of an object.'
myAccounting.Book.Accounts.Add(stdAccount);

解决方法

//compile time err: containing type does not implement interface 'IEnumerable'
IEnumerator IEnumerable.GetEnumerator() => Accounts.GetEnumerator();

这意味着您正在使用 explicit interface implementation。但是book类没有实现IEnumerable接口,所以会报错。

解决方案是实现接口

public class Book : IEnumerable

或者删除显式部分。请注意,这需要更改名称,因为您已经有一个具有相同签名的方法。

public IEnumerator GetNonGenericEnumerator() => Accounts.GetEnumerator();

但是正如评论中提到的那样。这可能不是解决问题的好方法。 IE。您应该使用组合而不是继承,即“一本书有一个帐户列表”,而不是“一本书是一个帐户列表”。

从这个问题中,你想搜索什么以及你期望什么结果并不明显。如果要搜索具有特定 ID 帐户的图书,可以使用:

myBooks.Where(b => b.Accounts.Any(a => a.Id == idToSearchFor));

但您需要公开 Id。如果您希望返回实际帐户,则可以使用:

myBooks.SelectMany(b => b.Accounts).Where(a => a.Id == idToSearchFor);