【问题标题】:CocoaPodHelper.cs `Process ` :type used in a using statement must be implicitly convertible to system.idisposableCocoaPodHelper.cs `Process` : 在 using 语句中使用的类型必须隐式转换为 system.idisposable
【发布时间】:2016-06-25 13:24:02
【问题描述】:

当我尝试在 Unity3d 中运行我的游戏时,它在编译器的 CocoaPodHelper.cs 文件中出现了这个错误

Process ` :type 在 using 语句中使用必须可以隐式转换为 system.idisposable

using (var process = new Process())”之后的任何内容都充满了错误。

这里是 cocapodhelper.cs 的代码

using System;
using System.Diagnostics;
using System.IO;

    namespace GoogleMobileAds
    {
        public class CocoaPodHelper
        {
            public static string Update(string projDir)
            {
                if (!Directory.Exists(projDir))
                {
                    throw new Exception("project not found: " + projDir);
                }

                string podPath = ExecuteCommand("which", "pod", null);
                if (podPath.Equals(""))
                {
                    throw new Exception("pod executable not found");
                }
                return ExecuteCommand(podPath.Trim(), "update", projDir);
            }

            private static string ExecuteCommand(string command, string argument, string workingDir)
            {

                using (var process = new Process())
                {
                    if (!process.StartInfo.EnvironmentVariables.ContainsKey("LANG"))
                    {
                        process.StartInfo.EnvironmentVariables.Add("LANG", "en_US.UTF-8");
                    }

                    string path = process.StartInfo.EnvironmentVariables["PATH"];
                    if(!path.Contains("/usr/local/bin"))
                    {
                        path = path + ":/usr/local/bin";
                        process.StartInfo.EnvironmentVariables.Remove("PATH");
                        process.StartInfo.EnvironmentVariables.Add("PATH", path);
                    }

                    if (workingDir != null)
                    {
                        process.StartInfo.WorkingDirectory = workingDir;
                    }
                    process.StartInfo.FileName = command;
                    process.StartInfo.Arguments = argument;
                    UnityEngine.Debug.Log("Executing " + command + " argument: " +
                        process.StartInfo.Arguments);
                    process.StartInfo.CreateNoWindow = true;
                    process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.RedirectStandardError = true;
                    process.StartInfo.RedirectStandardOutput = true;

                    try
                    {
                        process.Start();
                        process.StandardError.ReadToEnd();
                        var stdOutput = process.StandardOutput.ReadToEnd();
                        var stdError = process.StandardError.ReadToEnd();

                        UnityEngine.Debug.Log("command stdout: " + stdOutput);

                        if (stdError != null && stdError.Length > 0)
                        {
                            UnityEngine.Debug.LogError("command stderr: " + stdError);
                        }

                        if (!process.WaitForExit(10 * 1000))
                        {
                            throw new Exception("command did not exit in a timely fashion");
                        }

                        return stdOutput;

                    }
                    catch (Exception e)
                    {
                        throw new Exception("Encountered unexpected error while running pod", e);
                    }
                }
            }
        }
    }

【问题讨论】:

  • Process 类没有实现IDisposable 接口。

标签: c# unity3d admob


【解决方案1】:

using 语句允许在 using 语句结束后“处理”语句中的对象 - 该语句将调用一个方法来摆脱该对象。然而,System.Diagnostics.Process 并没有“拥有”允许将其处理掉的方法;它没有实现IDisposable 接口。所以,而不是: using (var process = new Process()) { //Do stuff here } 只是这样做: var process = new Process(); //Do stuff here 并且变量将在方法结束时超出范围。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多