如果我在这里遗漏了一些明显的东西,请寻求建议。 我怀疑它与参考组件有关。
我将 Unity 3D 用于与 SharePoint Online 通信的 Android 应用程序。
从 Unity 2022.3.2f1 升级到 2023.2.10f1 后,出现错误: 找不到 Microsoft.SharePoint.Client.ContentTypeId 类型的默认构造函数
尝试读取 SharePoint 列表时发生错误。
该错误仅在构建到设备(运行时)时显示在 Android Logcat 中。
2024/02/22 14:18:19.515 21552 21581 Error Unity MissingMethodException: Default constructor not found for type Microsoft.SharePoint.Client.ContentTypeId
2024/02/22 14:18:19.515 21552 21581 Error Unity at System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic, System.Boolean wrapExceptions) [0x00000] in <00000000000000000000000000000000>:0
2024/02/22 14:18:19.515 21552 21581 Error Unity at Microsoft.SharePoint.Client.ScriptTypeMap.CreateObjectFromScriptType (System.String scriptType, Microsoft.SharePoint.Client.ClientRuntimeContext context) [0x00000] in <00000000000000000000000000000000>:0
2024/02/22 14:18:19.515 21552 21581 Error Unity at Microsoft.SharePoint.Client.JsonReader.ReadJsonObject (System.Type fallbackType) [0x00000] in <00000000000000000000000000000000>:0
2024/02/22 14:18:19.515 21552 21581 Error Unity at Microsoft.SharePoint.Client.JsonReader.ReadKeyValue () [0x00000] in <00000000000000000000000000000000>:0
2024/02/22 14:18:19.515 21552 21581 Error Unity at Microsoft.SharePoint.Client.ListItem.InitNonPropertyFieldFromJson (System.String peekedName, Microsoft.SharePoint.Client.JsonReader reader) [0x00000] in <00000000000000000000000000000000>:0
2024/02/22 14:18:19.515 21552 21581 Error Unity at Microsoft.SharePoint.Client.ClientObject.FromJson (Microsoft.SharePoint.Client.JsonReader reader) [0x00000]
该项目已成功导入并升级(据我所知) 它在 Windows 上的 Unity 编辑器中运行完全正常。 仅当它“构建”到 Android 设备时才会出现该错误。
其他 SharePoint 功能,例如上传/下载文件、获取在线文件夹的目录列表或写入 SharePoint 列表,都可以工作。 唯一的问题是尝试读取 SharePoint 列表时。 所有这些代码之前已经工作了一年多,并且仍然可以在“旧”版本上工作。
public static void ReadListItem(string site, string listName, string camlViewXml)
{
clientContext = GetNewClientContext(site);
Microsoft.SharePoint.Client.List myList = clientContext.Web.Lists.GetByTitle(listName);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = camlViewXml;
tempListColl = myList.GetItems(camlQuery);
clientContext.Load(tempListColl);
try
{
clientContext.ExecuteQuery();
}
catch (WebException ex)
{
Debug.Log("List Update stopped:" + ex.Message);
}
catch (ServerException ex) { Debug.Log("List Update stopped:" + ex.Message); return; }
}
如果从异步任务调用也会发生该错误。
此代码在使用 .Net Standard 2.0 的旧版 Unity 上运行良好 该代码运行良好,并且可以在 Unity 编辑器本身中运行。
在新版本(仅允许 .net Standard 2.1)上,它在设备上构建和运行时会产生错误。 我相信这与 .dll 引用和依赖项有关,并且有些与标准 2.1 存在问题 我在其他地方看不到任何提及该错误的信息。据我从 Microsoft 文档得知,该函数应该仍然可以在 CSOM 库中工作。 当尝试简单地获取 SharePoint 列表项时会发生错误,这实际上是第一步。
我已经使用Nuget更新了软件包,问题仍然存在。除非我在这里做错了什么?
所以我今天发现这个问题与.Net根本无关。 它也不是 Microsoft.Sharepoint 程序集。
事实上,较新的 Unity 版本启用了“代码剥离”,这似乎会在构建期间删除它确定无法访问的代码。 https://docs.unity3d.com/Manual/ManagedCodeStripping.html
要更改“托管剥离级别”属性:
转到编辑>项目设置>播放器。 在“其他设置”中,导航至“优化”标题。 将“管理剥离级别”属性设置为所需的值。
将脚本切换回 Mono(已更改)允许您选择代码剥离的“禁用”选项。