簡體   English   中英

C# 安全異常

[英]C# Security Exception

運行此程序時,我不斷收到錯誤消息:發生了類型為“System.Security.SecurityException”的未處理異常附加信息:必須將 ECall 方法打包到系統模塊中。

 class Program{
        public static void Main()
        {
            Brekel_ProBody2_TCP_Streamer s = new Brekel_ProBody2_TCP_Streamer();
            s.Start();
            s.Update();
            s.OnDisable();
        }
    }

我怎樣才能解決這個問題?

Brekel庫的重要部分如下:

//======================================
    // Connect to Brekel TCP network socket
    //======================================
    private bool Connect()
    {
        // try to connect to the Brekel Kinect Pro Body TCP network streaming port
        try
        {
            // instantiate new TcpClient
            client = new TcpClient(host, port);

            // Start an asynchronous read invoking DoRead to avoid lagging the user interface.
            client.GetStream().BeginRead(readBuffer, 0, READ_BUFFER_SIZE, new AsyncCallback(FetchFrame), null);

            Debug.Log("Connected to Brekel Kinect Pro Body v2");
            return true;
        }
        catch (Exception ex)
        {
            Debug.Log("Error, can't connect to Brekel Kinect Pro Body v2!" + ex.ToString());
            return false;
        }
    }


    //===========================================
    // Disconnect from Brekel TCP network socket
    //===========================================
    private void Disconnect()
    {
        if (client != null)
            client.Close();
        Debug.Log("Disconnected from Brekel Kinect Pro Body v2");
    }

 public void Update()
{
    // only update if connected and currently not updating the data
    if (isConnected && !readingFromNetwork)
    {
        // find body closest to the sensor
        closest_skeleton_ID = -1;
        closest_skeleton_distance = 9999999f;
        for (int bodyID = 0; bodyID < skeletons.GetLength(0); bodyID++)
        {
            if (!skeletons[bodyID].isTracked)
                continue;
            if (skeletons[bodyID].joints[(int)brekelJoint.waist].position_local.z < closest_skeleton_distance)
            {
                closest_skeleton_ID = bodyID;
                closest_skeleton_distance = skeletons[bodyID].joints[(int)brekelJoint.waist].position_local.z;
            }
        }

        // apply to transforms (cannot be done in FetchFrame, only in Update thread)
        for (int bodyID = 0; bodyID < skeletons.GetLength(0); bodyID++)
        {
            for (int jointID = 0; jointID < skeletons[bodyID].joints.GetLength(0); jointID++)
            {
                // only apply if transform is defined
                if (skeletons[bodyID].joints[jointID].transform != null)
                {
                    // apply position only for waist joint
                    if (jointID == (int)brekelJoint.waist)
                        skeletons[bodyID].joints[jointID].transform.localPosition = skeletons[bodyID].joints[jointID].position_local;

                    // always apply rotation
                    skeletons[bodyID].joints[jointID].transform.localRotation = skeletons[bodyID].joints[jointID].rotation_local;
                }
            }
        }

您似乎正在使用Unity庫但嘗試將其作為獨立應用程序運行?

此錯誤意味着您正在調用Unity引擎中實現的方法。 您只能在Unity中使用該庫。

如果你想獨立使用它,你需要編譯庫而不引用任何Unity庫,這可能意味着你需要為庫所使用的任何東西提供實現(例如MonoBehaviour

http://forum.unity3d.com/threads/c-error-ecall-methods-must-be-packaged-into-a-system-module.199361/

http://forum.unity3d.com/threads/security-exception-ecall-methods-must-be-packaged-into-a-system-module.98230/

您可以通過打開並使用Unity測試運行器在Unity中編寫測試,可以通過轉到窗口並按下測試運行器來打開它。 然后點擊create editmode test並在你創建的文件中寫下你的測試。

https://docs.unity3d.com/Manual/testing-editortestsrunner.html

這樣你就不需要省略unity的庫的使用。

另外,如果您唯一的問題是Debug.Log()拋出異常,您可以使用反射來構建您自己的Logger實例而不是Unity的實例。

步驟1:創建“MyLogHandler”,它將執行您的實際日志記錄(寫入文件或控制台或不執行任何操作)。 您的類需要實現“ILogHandler”接口。

第2步:用新的替換單位默認值。

var newLogger = new Logger(new MyLogHandler());
var fieldInfo = typeof(Debug).GetField("s_Logger", BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static);
        fieldInfo.SetValue(null, newLogger);

注意:請記住,反射按名稱訪問字段,如果Unity決定將來更改它,您將不會遇到編譯錯誤 - 將在運行時拋出異常。

我知道這是舊的,但我偶然通過從Unity構建設置中切換符號定義來從Visual Studio中對Unity程序集進行單元測試。 只要你能夠既可以運行測試,也可以讓可測試組件一次性使用統一,你就可以像這樣切換單元測試模式和統一模式(圖片如下):

  1. 使你的團結組成部分類。 有一個文件,你聲明部分類擴展MonoBehaviour並放置任何必須實際使用統一集合的東西。 這不會通過單元測試來測試,但其他一切都會測試。
  2. 使用條件編譯使該文件的內容僅在構建期間定義特定符號時進行編譯。 在我的案例中我使用了UNIT_TEST_NO_UNITY_INTEGRATION
  3. 如果要從Visual Studio運行單元測試,請更新構建設置以定義該符號。 這將從構建中排除步驟1中的Unity特定內容,並允許Visual Studio運行單元測試。
  4. 完成測試后,再次編輯構建設置並刪除該符號定義。 現在您的單元測試將無法運行,但您的程序集將再次在Unity中運行。

在此輸入圖像描述 在此輸入圖像描述

暫無
暫無

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

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