简体   繁体   English

Unity错误:.ctor():方法主体为空

[英]Unity Error: .ctor(): method body is empty

This is a follow up question, coming from my last question about how to read and write from streams. 这是一个后续问题,来自我上一个有关如何从流读取和写入的问题 The code that follows is the best answer from there: 下面的代码是最好的答案:

    var byteData = Encoding.UTF8.GetBytes(data);

    var saveFileDialog = new SaveFileDialog
    {
        DefaultExt = "json",
        AddExtension = true,
        Filter = "JSON|*.json"
    };

    if (saveFileDialog.ShowDialog() != DialogResult.OK ||
        string.IsNullOrEmpty(saveFileDialog.FileName)) return;

    using (var saveFileDialogStream = saveFileDialog.OpenFile())
    {
        saveFileDialogStream.Write(byteData, 0, byteData.Length);
    }

The person who gave this to me confirms that it works outside of Unity, however when I try to have it run through Unity it throws the following error: 给我的人确认它可以在Unity之外运行,但是当我尝试通过Unity运行它时,会引发以下错误:

InvalidProgramException: Invalid IL code in System.Forms.SaveFileDialog: .ctor (): method body is empty InvalidProgramException:System.Forms.SaveFileDialog中的无效IL代码:.ctor():方法主体为空

Any ideas? 有任何想法吗? If you would like more details just ask. 如果您想了解更多详细信息,请询问。 I'll do my best to flesh out my issue. 我会尽力充实我的问题。

EDIT 编辑

Fixed first error it seems, but this uncovered a second, taking issue with the if statement seen above: 修复了似乎出现的第一个错误,但是发现了第二个错误,上面的if语句出现了问题:

NullReferenceException: Object reference not set to an instance of an object NullReferenceException:对象引用未设置为对象的实例

SaveFileDialog class requires .NET Framework 1.1 or superior in order to run, as per pointed out at the end page of the documentation you have linked to in your original post. SaveFileDialog类需要.NET Framework 1.1或更高版本才能运行,如在原始文章中链接到文档末页所指出的那样。

.NET Framework is one of several runtimes provided in the .NET environment. .NET Framework是.NET环境中提供的几种运行时之一。 Problem is: Unity does not use .NET Framework as its main runtime - it uses Mono , which is a lighter and portable version of .NET runtime, designed to run in multiple platforms. 问题是:Unity 不使用.NET Framework作为其主要运行时,而是使用MonoMono是.NET运行时的轻便可移植版本,旨在在多个平台上运行。 This is how Unity is able to execute .NET/C# code in iOS, Android, PC, macOS, etc. 这就是Unity能够在iOS,Android,PC,macOS等系统中执行.NET / C#代码的方式。

So basically you cannot use the SaveFileDialog class in Unity without messing around with plugins which provide platform-specific implementations of a dialog for saving files. 因此,基本上,您不能在Unity中使用SaveFileDialog类,而不能弄乱插件,这些插件提供了平台特定的对话框来实现文件保存。 You would have to write a plugin which could access that class, and even then, it would only be able to be executed in Windows-based platforms, as .NET Framework is a Windows-specific .NET runtime . 您将不得不编写一个可以访问该类的插件,即使那样,它也只能在基于Windows的平台上执行,因为.NET Framework是特定于Windows的.NET运行时

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM