c# - Unity - Windows 中出现“空对象引用”错误,但 macOS 中没有
问题描述
我正在与其他开发人员一起制作 2D 游戏。我是唯一一个使用 macOS 机器的人,而我的其他朋友正在使用 Windows 机器。
我做了一个递归函数,从对象中提取底层原始字段及其数据。
在处理 _syncroot 变量时,遇到传递具有空 List<> 对象(包含 0 个项目的列表)的对象时似乎会导致错误。
public List<KeyValuePair<FieldInfo, object>> FlattenAndGetPrimitiveFields(object o)
{
List<KeyValuePair<FieldInfo, object>> l = new List<KeyValuePair<FieldInfo, object>>();
foreach (FieldInfo f in o.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
{
if (!f.FieldType.IsPrimitive && f.FieldType != typeof(string) && f.FieldType.BaseType != typeof(System.Enum))
{
//Debug.Log(f);
//Debug.Log(o);
object nonPrimitiveField = f.GetValue(o);
// Can use the AddRange method to make this faster.
l = l.Concat(FlattenAndGetPrimitiveFields(nonPrimitiveField)).ToList();
}
else
{
KeyValuePair<FieldInfo, object> p = new KeyValuePair<FieldInfo, object>(f,o);
l.Add(p);
}
}
return l;
}
我只想获取底层的原始字段 - 但是,此代码已经适用于我的 macOS 机器,而不适用于我朋友的 Windows 机器。
他们正在运行 .NET Framework,而我安装了 Mono Framework 和 .NET Core。
解决方案
推荐阅读
- python - 将json文件转换成txt文件目录
- r - 如何在 Ggplot 中对 y 轴进行排序?
- robotframework - 使用机器人框架打开应用程序时 Appium 出现错误
- google-chrome-extension - 当用户单击活动网页中的元素时,如何打开扩展弹出窗口?
- java - Spring ControllerAdvice 中未处理 404 异常
- css - 缩放具有不同宽度和高度的动态图像
- javascript - 在不清除其他内容的情况下更改元素的文本
- python - 导入调用中的模拟方法调用
- javascript - 同一 Provider 的多个声明是否会在 React 中创建新实例?
- c# - 使用 Visual Studio 在 C# 中上传 Excel 工作表