Search Unity

Problem with external .DLL

Discussion in 'Scripting' started by hydudajan, May 30, 2017.

  1. hydudajan

    hydudajan

    Joined:
    Oct 2, 2016
    Posts:
    6
    Hello,
    I have created a DLL which contains my logic. I could import the DLL successfully.
    But when i call the LoadData method from my Editorscript Unity crashes when the LoadData method returns without an error or something. In the Editor Log i can see the Debugs are all called. So i think when LoadData returns there is some error but i cant find anything.

    LoadData is a function from my external DLL.

    Of course all my functions are in classes. I just copied the important parts.

    Code (CSharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using System.Xml.Serialization;
    5. using System.Collections.Generic;
    6. using System.IO;
    7. using System.Text;
    8. using System.Xml;
    9.  
    10.  
    11.         // From External DLL
    12.         public static Language LoadData(string filePath)
    13.         {
    14.             try
    15.             {
    16.                 // When File exist
    17.                 if (File.Exists(filePath))
    18.                 {
    19.                     // create a variable to store the result
    20.                     Language result;
    21.  
    22.                     // create a TextReader
    23.                     StreamReader textReader = new StreamReader(filePath, true);
    24.  
    25.                     // create a XmlReader
    26.                     XmlReader xmlReader = XmlReader.Create(textReader);
    27.  
    28.                     // create a XmlSerializer
    29.                     XmlSerializer xmlSerializer = new XmlSerializer(typeof(Language));
    30.  
    31.                     Debug.Log("Vorbereitung");
    32.  
    33.                     // When xmlSerializer can Deserialize xmlReader
    34.                     if (xmlSerializer.CanDeserialize(xmlReader))
    35.                     {
    36.                         Debug.Log("Deserialisieren");
    37.                         // store the object as result
    38.                         result = xmlSerializer.Deserialize(xmlReader) as Language;
    39.  
    40.                         Debug.Log("fertig Deserialisiert");
    41.                         // close the textreader
    42.                         textReader.Close();
    43.  
    44.                         Debug.Log("Return");
    45.  
    46.                         // return object
    47.                         return result;
    48.                     }
    49.                 }
    50.                 // return default object
    51.                 return null;
    52.             }
    53.             catch(UnityException ex)
    54.             {
    55.                 Debug.LogException(ex);
    56.             }
    57.  
    58.                 return null;
    59.         }
    60.  
    61.  

    Code (CSharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using UnityEditor;
    5. using System.IO;
    6. using LocalizationData;
    7. using System.Collections.Generic;
    8.  
    9.     private void OnGUI()
    10.     {
    11.         GUIIsFileSelected();
    12.     }
    13.  
    14.     private void GUIIsFileSelected()
    15.     {
    16.         // When no File is selected
    17.         if (!bFileSelected)
    18.         {
    19.             // Show a Load File Button
    20.             if (GUILayout.Button("Load File"))
    21.             {
    22.                 // When the button is clicked open a select file dialog
    23.                 string filePath = SelectFile();
    24.  
    25.                 Debug.Log("File path: " + filePath);
    26.  
    27.                 // When string is not null or empty
    28.                 if (!string.IsNullOrEmpty(filePath))
    29.                 {
    30.                     Debug.Log("not null or empty");
    31.                     SelectedLanguage = Language.LoadData(filePath);
    32.                     Debug.Log("Set file selected");
    33.                     SetFileSelected(true);
    34.                 }
    35.                 // When string is null or empty
    36.                 else
    37.                 {
    38.                     Debug.LogWarning("Selected File is not valid.");
    39.                 }
    40.             }
    41.  
    42.             return;
    43.         }
    44.     }
    45.  
     
  2. hydudajan

    hydudajan

    Joined:
    Oct 2, 2016
    Posts:
    6
    In this moment I found the error. It was the declaration of my property.
    Can someone explain it to me?

    Code (CSharp):
    1.  
    2.  
    3.     // This work's fine
    4.     public Language SelectedLanguage { get; set; }
    5.  
    6.     // This didn't work
    7.     public Language SelectedLanguage
    8.     {
    9.         get
    10.         {
    11.             return SelectedLanguage;
    12.         }
    13.         set
    14.         {
    15.             SelectedLanguage = value;
    16.         }
    17.     }
    18.  
    19.  
     
  3. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Endless recursion (or stack overflow - depends on the situation) . The getter is defined in such a way that is calls itself. Setter calls itself as well.

    You either just declare get;set; if no further implementation detail is needed so far, or you declare the backing field(s) which are supposed to be accessed by the property.
     
  4. RPKMN1

    RPKMN1

    Joined:
    Feb 13, 2017
    Posts:
    36
    if you're using the
    the first section is what's called an auto-implemented property.

    in your "This didn't work" section, you've told the compiler you're taking over implementation of the property. then you tell it to return itself. that's the recursion suddoha is talking about. Change it to use a separate variable like _selectedLanguage for both the getter and setter.

    or just use the first one.