无法从数组中的 class 访问列表

Cannot access List from a class within an Array

我是一名开发新手,如果您能帮我找出代码中的问题,我将不胜感激。代码特别混乱,主要是因为它是从一个框架派生出来的。评论应该可以让我们有所了解。

// Create an IBindable List
public static List<IBindable> KyzerBindables = new List<IBindable>();

// Attach elements to a list, for better control over all of them
internal static void AttachBindablesToList(IReadOnlyList<Drawable> children)
{
    // For all the children classes located in Drawable list
    for (int i = 0; i < children.Count; i++) // children.Count returns 4
    {
        // For all of the SettingsSubsection which are present in the Drawable array
        for (int l = 0; l < (children[i] as SettingsSubsection).Children.Count; l++) // (children[i] as Subsection).Children.Count returns 0.
        {
            // Get a specific element
            var element = (children[i] as SettingsSubsection).Children[l];

            // if is a SettingsCheckbox
            if (element.GetType() == typeof(SettingsCheckbox))
                KyzerBindables.Add((element as SettingsCheckbox).Bindable);
        }
    }
}
// in another class
public class KyzerSection: SettingsSection
{
    public KyzerSection()
    {
        Children = new Drawable[]
        {
            new KyzerMiscellaneous(),
        };

        ...AttachElementsToList(Children);
    }
}

public class KyzerMiscellaneous: SettingsSubsection
{
    [BackgroundDependencyLoader] // Calls load, framework thing.
    private void load(OsuConfigManager config)
    {
        Children = new Drawable[]
        {
            new SettingsCheckbox
            {
                LabelText = "Something here",
                Bindable = new BindableBool(false),
            }
        };
    }
}

我的问题是,第二个 for 循环甚至没有为 AttachBindablesToList 启动。无论出于何种特殊原因,它都没有收到计数。我不确定我做错了什么。

编辑:

如果 GitHub 存储库问题以任何方式解决了一些问题,请随时导航到那里并检查包含这些更改的提交。 https://github.com/Frontear/osuKyzer/issues/3

查看您的 github 存储库后,我认为问题出在:

private void load(params here)

在 AttachBindablesToList 时没有调用上面的方法。这导致空

(children[i] as SettingsSubsection).Children.Count

最好的选择是创建一个空的实例化方法

public KyzerMiscellaneous() { /* create Drawable elements */ }
// then
[BackgroundDependancyLoader]
private void load(params here) { /* doSomething */ }

这将允许访问子列表,因为它之前已经初始化,因此允许第二个循环正确运行,并将 IBindables 推送到您的列表。