c# - 在 dll 项目中使用递归函数
问题描述
好的,所以我创建了一个 dll 项目。此 dll 项目使用树类结构来保存数据,并具有用于处理该数据的函数。
所以在 dll 项目中我们有:
namespace Engine
{
public class Engineclass
{
public Component SolutionTree = new Component();
public Component ReturnComponent(string ComponentName)
{
Component CurrentComp = SolutionTree;
if(CurrentComp.Name != ComponentName)
{
CurrentComp = RecurseForName(CurrentComp, ComponentName);
}
return CurrentComp;
}
public Component RecurseForName(Component CompORG, ComponentName)
{
Component FoundComp = null;
if (!string.IsNullOrEmpty(ComponentName) && (CompORG != null))
{
if (CompORG.ComponentName.ToUpper().Trim() ==
ComponentName.ToUpper().Trim())
{
return CompORG;
}
foreach (var CompNew in CompORG.ActiveComponents)
{
var FoundComp2 = RecurseForName(CompNew, ComponentName);
if (FoundComp2 != null)
{
return FoundComp2;
}
}
}
return FoundComp;
}
}
}
组件类是:
public class Component
{
public string Name {get; set;}
public List<Component> ActiveComponents { get; set; }
public Component()
{
this.ActiveComponents = new List<ActiveComponent>();
}
}
现在在我的控制台应用程序中我正在对其进行测试,我加载 dll 并填充解决方案树。IE
EngineClass EC = new EngineClass();
EC.Component NewComp = new EC.Component();
EC.Name = "bob";
EC.Component NewSubComp = new EC.Component();
EC.Name = "Son of Bob";
NewComp.ActiveComponents.add(NewSubComp);
EC.SolutionTree = NewComp;
一切正常。
但是,当我尝试使用递归函数返回特定组件时,我得到一个 System.NullReferenceException:'对象引用未设置为对象的实例。
var FoundASon = EC.FindComponent("Son of Bob");
它发生在函数的return CompORG;
行上。RecurseForName
所以,我知道CompOrg
不是null,而且里面的名字CompOrg
已经匹配,但是试图返回CompOrg
会导致nullreferenceexception!
我真的需要帮助解决这个问题,因为调试已编译的 dll 非常困难。
ps dll 项目有单元测试,它们通过功能正常工作。
解决方案
推荐阅读
- javascript - 触发器不会在 Realm/MongoDB 中返回正确的用户对象
- linux - 树莓派 uhubctl 权限
- android - 检测 API 响应变化的最佳方法
- java - Android Studio - 应用程序在启动时崩溃,我不知道这个错误是什么意思:引起:java.lang.ClassNotFoundException:[...]
- macos - 错误:命名空间“std”中没有名为“async”的成员
- regex - 大查询 REGEXP_REPLACE 文本转整数
- java - ScheduledExecutorService 无法正常工作
- python - CSRF 中间件令牌丢失?
- python - 在消息事件上使我的机器人无法工作
- reactjs - 在 Electron/NextJS/ReactJs 中具有不同域的 reCAPTCHA