簡體   English   中英

無法將 System.Threading.Tasks.Task... 轉換為 System.Threading.Tasks.Task... 盡管看似相同的類型

[英]Cannot convert System.Threading.Tasks.Task… to System.Threading.Tasks.Task… despite seemingly being same type

我在 Rider 中遇到了一個奇怪的錯誤,因為我試圖以一種不允許的方式轉換為 Task。 奇怪的是,這些類型似乎完全匹配。

代碼拋出錯誤

        public IEnumerator GetCatalogItem_LevelDataId_PlayFabCatalogItemFound(string levelName,long levelDataId)
        {
            //Arrange
            const string getCatalogItemCloudFunctionName = "getCatalogItem";
            var data = new Dictionary<string, object>
            {
                {"itemId", levelDataId.ToString()}
            };
            //Act
            var result = FirebaseFunctions.DefaultInstance
                .GetHttpsCallable(getCatalogItemCloudFunctionName)
                .CallAsync(data);
            //BUG ERROR IS SHOWN ON result BELOW (SEE ATTACHED IMAGE)
            yield return result.AsIEnumerator<HttpsCallableResult>();
            var catalogItem = (IDictionary)result.Result.Data;
            //Assert
            Assert.AreEqual(levelName,catalogItem["DisplayName"].ToString());
        }

使用的任務擴展方法

    public static class TaskExtensions
    {
        /// <summary>
        /// Converts a <see cref="System.Threading.Tasks.Task"/> to <see cref="IEnumerator"/> (helps with unity coroutines)
        /// </summary>
        /// <param name="task">The task to convert</param>
        /// <returns>An IEnumerator which waits till the task has been completed</returns>
        public static IEnumerator AsIEnumerator(this Task task)
        {
            while (!task.IsCompleted)
            {
                yield return null;
            }

            if (task.IsFaulted)
            {
                throw task.Exception;
            }
        }
        
        /// <inheritdoc cref="AsIEnumerator"/>
        public static IEnumerator AsIEnumerator<T>(this Task<T> task)
        {
            while (!task.IsCompleted)
            {
                yield return null;
            }

            if (task.IsFaulted)
            {
                throw task.Exception;
            }
        }
    }

拋出錯誤

無法將實例參數類型 'System.Threading.Tasks.Task<Firebase.Functions.HttpsCallableResult> [Firebase.Functions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]' 轉換為 'System.Threading.Tasks.Task< Firebase.Functions.HttpsCallableResult> [mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089]'

任務錯誤圖像

Firebase 根據他們的文檔提供了自己的TaskTask<T>實現: https://firebase.google.com/docs/reference/unity/class/system/threading/tasks/task

看起來您需要在代碼中更明確地使用哪一個,以便將正確的一個解析為您希望使用的參考。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM