簡體   English   中英

C#-Unity內部編譯器錯誤:“ System.Runtime.CompilerServices.CallSite” CS0518-使用動態變量時

[英]C# - Unity Internal Compiler Error: “System.Runtime.CompilerServices.CallSite” CS0518 - when using dynamic variable

使用MonoDevelop 4.0.1為Unity 4.5.2f1編碼

下面的代碼是我正在使用的代碼的一部分,所有變量都已使用Debug.Log進行了檢查,並且它們全部(單獨地)返回正確的值。

當我在Unity中刷新腳本資產時,收到以下錯誤消息:

內部編譯器錯誤。 有關更多信息,請參見控制台日志。 輸出為:錯誤CS0518:未定義或導入了預定義類型“ System.Runtime.CompilerServices.CallSite”

錯誤CS0518:未定義或導入了預定義類型System.Runtime.CompilerServices.CallSite 1'

錯誤CS0518:未定義或導入了預定義類型'System.Runtime.CompilerServices.CallSite'

錯誤CS0518:未定義或導入了預定義類型System.Runtime.CompilerServices.CallSite 1'

錯誤CS0518:未定義或導入了預定義類型'System.Runtime.CompilerServices.CallSite'

錯誤CS0518:未定義或導入了預定義類型System.Runtime.CompilerServices.CallSite 1'

錯誤CS0518:未定義或導入了預定義類型'System.Runtime.CompilerServices.CallSite'

錯誤CS0518:未定義或導入了預定義類型System.Runtime.CompilerServices.CallSite 1'

錯誤CS0518:未定義或導入了預定義類型'System.Runtime.CompilerServices.CallSite'

錯誤CS0518:未定義或導入了預定義類型System.Runtime.CompilerServices.CallSite 1'

錯誤CS0518:未定義或導入了預定義類型'System.Runtime.CompilerServices.CallSite'

錯誤CS0518:未定義或導入了預定義類型System.Runtime.CompilerServices.CallSite 1'

這是我的腳本(簡體)中導致此錯誤的部分:

注意:我無法將我的功能分解為多個小功能。 我的功能必須保持不變,因為我可以根據需要編輯return語句

using UnityEngine;
using System.Collections;
using Random = UnityEngine.Random;

public class myScene : MonoBehaviour
{
    private dynamic someVar;
    private float myFloat = 1.1f;
    private string myString = "string";
    private int myInt = 2;

    void OnGUI()
    {
        someVar = myFunction(myFloat, myString, myInt);
    }

    //First Function
    public dynamic myFunction(float myFloat, string myString, int myInt)
    {
        //Do something
        dynamic myOtherFunction(myFloat, myString, myInt);
        float myFloat = myOtherFunction.myFloat;
        string myString = myOtherFunction.myString;
        int myInt = myOtherFunction.myInt;
        //Do something
        return new {myFloat = myFloat, myString = myString, myInt = myInt};
    }

    //Second function
    public dynamic myOtherFunction(float myFloat, string myString, int myInt)
    {
        //Do something
        return new {myFloat = myFloat, myString = myString, myInt = myInt};
    }
}

我無法弄清楚問題出在哪里(是的,即使使用了Google,盡管提出了一些可能的解決方案,但我還是嘗試了它們,但似乎無法解決我的問題)。

我需要能夠從另一個函數向OnGUI()函數返回多個變量。 我正在使用的方法return new{varName = value, ...}; 我在這里找到的。 我不能使用元組,因為Unity 4 MonoDevelop在用戶發布的地方不支持它(根據這篇文章):

不,Unity不支持元組。 也許當Unity升級其Mono版本以支持.NET Framework 4中的功能時。

最終,我想在OnGUI()函數中實現以下代碼:

myFloat = myFunction.myFloat;
myString = myFunction.myString;
myInt = myFunction.myInt

非常感謝您的任何幫助,謝謝

您遇到了與Tuples相同的問題。 dynamic是在.Net Framework 4.0中引入的,並且需要動態語言運行庫(DLR),而在Unity的Mono版本中則沒有。

話雖如此,給定上下文,您在這里的注釋表明“我的功能必須保持原樣,我可以根據需要編輯return語句”表明您根本不需要當前設置。 真的不明白為什么您不能使用對象來保存值(示例中使用的名稱很難給出邏輯名稱):

    public class MyFunctionResult
    {
        public readonly float MyFloat;
        public readonly string MyString;
        public readonly int MyInt;

        public MyFunctionResult(float myFloat, string myString, int myInt)
        {
            MyFloat = myFloat;
            MyString = myString;
            MyInt = myInt;
        }
    }

    void OnGUI()
    {
        float myFloat = 1;
        string myString = "";
        int myInt = 2;
        var finalResult = myFunction(myFloat, myString, myInt);
        Debug.Log(finalResult.MyFloat); //...
    }

    //First Function
    public MyFunctionResult myFunction(float myFloat, string myString, int myInt)
    {
        //Do something
        var myOtherFunctionResult = myOtherFunction(myFloat, myString, myInt);
        float myFloatResult = myOtherFunctionResult.MyFloat;
        string myStringResult = myOtherFunctionResult.MyString;
        int myIntResult = myOtherFunctionResult.MyInt;
        //Do something
        return new MyFunctionResult(myFloat, myString, myInt);
    }

    //Second function
    public MyFunctionResult myOtherFunction(float myFloat, string myString, int myInt)
    {
        //Do something
        return new MyFunctionResult(myFloat, myString, myInt);
    }

非常努力地提出了一個甚至微不足道的正當理由,但是沒有(但是我想知道您的簡化是否隱藏了一個隱藏的問題)。 話雖如此,您也可以使用out參數,但是我肯定會稱其為濫用:

public dynamic myFunction(float myFloat, string myString, int myInt)
{
    //Do something

    float myFloat;
    string myString;
    int myInt;
    myOtherFunction(out myFloat,out myString,out myInt);
    ...
}
public void myOtherFunction(out float myFloat, out string myString, out int myInt)
{
    myFloat = someVal;
    myString = otherVal;
    myInt = thirdVal;

    return;
}

暫無
暫無

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

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