Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
构造函数中的C#Foreach循环迭代一次_C#_Json_Json.net - Fatal编程技术网

构造函数中的C#Foreach循环迭代一次

构造函数中的C#Foreach循环迭代一次,c#,json,json.net,C#,Json,Json.net,我正在制作自定义JSON文件编辑器。 为了更好地管理数据,我想创建一个树。 默认JToken和JObject对我来说太复杂了,无法使用它们来管理数据。这就是为什么我想将从输入文件创建的JObject转换为树 我的准则的基本原则是: 将JToken传递给容器对象的构造函数; 容器对象包含(容器类型的)子容器列表; 如果JToken是复杂的而不是原始的,那么它将遍历该JToken中的所有子JToken JToken,它使用该子JToken创建新容器,并将其添加到子容器列表中。如果输入JToken是原

我正在制作自定义JSON文件编辑器。 为了更好地管理数据,我想创建一个树。 默认JToken和JObject对我来说太复杂了,无法使用它们来管理数据。这就是为什么我想将从输入文件创建的JObject转换为树

我的准则的基本原则是: 将JToken传递给容器对象的构造函数; 容器对象包含(容器类型的)子容器列表; 如果JToken是复杂的而不是原始的,那么它将遍历该JToken中的所有子JToken JToken,它使用该子JToken创建新容器,并将其添加到子容器列表中。如果输入JToken是原语,它会保存它的值并停止递归

public class Container
{
    protected GraphicalBlock graphicalBlock;
    //Universal usage
    protected bool IsOriginObject;

    //For primitive containers
    protected object PrimitiveContent;

    //For complex containers
    protected Container child_container;
    protected List<Container> child_elements;

    /// <summary>
    /// Constructor when provided with raw jobject will create a tree that allows easy data managment.
    /// Constructor is only called once, after that the whole tree will be created recusivly
    /// </summary>
    /// <param name="input_jobject"></param>
    /// <param name="Is_orig_obj"></param>
    public Container(JToken input_jobject, Point loc, Form main_form_ref, bool Is_orig_obj=true)
    {
        IsOriginObject = Is_orig_obj;

        //Check for complex vs primitive
        if(input_jobject.HasValues)
        {
            //Populate children list with next level/tier tokens
            //Every token will execute the same procedure eventualy creating a tree
            //The main container is the origin of the tree, primitive containers are the end of the branches
            int i = 0;
            JObject job = input_jobject as JObject;
            foreach (KeyValuePair<string, JToken> pair in job)
            {
                child_elements.Add(new Container(pair.Value, new Point(loc.X + i, loc.Y+120) ,main_form_ref, false));
                i=+120;
            }

            graphicalBlock = new GraphicalBlock(loc, true, main_form_ref, this);
        }
        else
        {
            //If token is primitive we can extract value from it
            PrimitiveContent = input_jobject.ToObject<object>();
            graphicalBlock = new GraphicalBlock(loc, false, main_form_ref, this);
        }
    }
}
公共类容器
{
受保护的GraphicalBlock GraphicalBlock;
//普遍使用
受保护的布尔同构对象;
//对于原始容器
受保护对象基本内容;
//对于复杂容器
受保护容器子容器;
受保护的列表子元素;
/// 
///当与原始jobject一起提供时,构造函数将创建一个允许轻松管理数据的树。
///构造函数只被调用一次,之后整个树将被重复创建
/// 
/// 
/// 
公共容器(JToken input\u jobject,Point loc,Form main\u Form\u ref,bool Is\u orig\u obj=true)
{
IsOriginObject=Is_orig_obj;
//检查是否存在复杂与基本体
if(输入_jobject.HasValues)
{
//用下一级/层标记填充子列表
//每个令牌最终将执行相同的过程来创建一个树
//主容器是树的原点,原始容器是分支的末端
int i=0;
JObject job=输入_JObject作为JObject;
foreach(作业中的KeyValuePair对)
{
子元素。添加(新容器(pair.Value,新点(loc.X+i,loc.Y+120),主形式参考,false));
i=+120;
}
graphicalBlock=新的graphicalBlock(loc,true,主表格,本);
}
其他的
{
//如果令牌是原语,我们可以从中提取值
PrimitiveContent=input_jobject.ToObject();
graphicalBlock=新的graphicalBlock(loc,false,主表格,本);
}
}
}
graphicalBlock对象只是我用来添加GUI的一个奇特的包装器。 我认为这不会影响循环

主要问题是,在第一次迭代和创建第一个子容器之后,循环以某种方式中断。有人能解释为什么foreach循环在第一个令牌之后中断吗?(否输入JToken不包含仅1个值)

如果有人对我的代码有任何疑问,请不要犹豫。 我的问题非常具体,所以我无法在谷歌上或使用搜索工具找到它。

这行

protected List<Container> child_elements;
您成功创建了该子容器,但由于要添加到的列表不存在而失败。您可能得到了一个以某种方式隐藏的NullReferenceException(尝试/捕获调用方?)

解决方案:通过如下方式声明字段,确保该字段确实包含列表:

protected List<Container> child_elements = new List<Container>();
受保护的列表子元素=新列表();

循环由于某种原因而中断?
调试器说什么?一切都能完美编译。问题是循环只进行一次迭代。JToken的值大于1。我想让循环遍历所有这些代码。将军问的是调试器,而不是编译器。转到foreach的第一行,点击F9,这将放置一个断点。运行代码。当它停止时,使用F11键进行单步操作,并将鼠标悬停在变量上以查看它们所持有的内容。集合将允许您深入查看它们的计数。诸如此类的事情。在您的代码库中,最强大的工具之一就是使用调试器的知识。很抱歉,我误解了他的评论。当我使用调试器时,循环设法创建对象,子对象构造函数然后通过else部分(因为Jtoken中的第一个对象是基元对象),它返回到循环,然后循环中断。偶i+=120;未执行。因此,当您调试时。你认为这份工作的价值是多少?我忘了。。。谢谢你的帮助!我仍然不知道为什么它默默地失败了。
protected List<Container> child_elements = new List<Container>();