简体   繁体   English

非静态字段,方法或属性需要对象引用

[英]An object reference is required for the non-static field, method, or property

I wrote a very small function to start a Java application in C# NET, but I am getting the error "An object reference is required for the non-static field, method, or property 'MinecraftDaemon.Program.LaunchMinecraft()' C:\\Users\\Mike\\Desktop\\Minecraft\\MinecraftDaemon\\Program.cs". 我写了一个非常小的函数来启动C#NET中的Java应用程序,但是我收到错误“非静态字段,方法或属性需要对象引用'MinecraftDaemon.Program.LaunchMinecraft()'C:\\用户\\麦克\\桌面\\我的世界\\ MinecraftDaemon \\的Program.cs”。 I have searched other threads that suffer from the same issue but I don't understand what it means or why I am getting it. 我搜索过遭受同样问题的其他主题,但我不明白这意味着什么或为什么我会得到它。

namespace MinecraftDaemon
{
    class Program
    {
        public void LaunchMinecraft()
        {
            ProcessStartInfo processInfo = new ProcessStartInfo("java.exe", "-Xmx1024M -Xms1024M -jar minecraft_server.jar nogui");
            processInfo.CreateNoWindow = true;
            processInfo.UseShellExecute = false;

            try
            {
                using (Process minecraftProcess = Process.Start(processInfo))
                {
                    minecraftProcess.WaitForExit();
                }
            }
            catch
            {
                // Log Error
            }
        }

        static void Main(string[] args)
        {
            LaunchMinecraft();
        }
    }
}

You need to change it to: 您需要将其更改为:

public static void LaunchMinecraft()

That way, the static Main method can access the static LaunchMinecraft method. 这样,静态Main方法可以访问静态LaunchMinecraft方法。

LaunchMinecraft is not a static method so you cannot access it in the static method Main without calling it from the Program object. LaunchMinecraft不是静态方法,因此您无法在静态方法Main访问它,而无需从Program对象调用它。

Two options 两种选择
1. Make LaunchMinecraft static 1.使LaunchMinecraft静态

public void LaunchMinecraft() 
{ ... }  

2. Create a new Program object in Main and call it that way. 2.在Main创建一个新的Program对象并以此方式调用它。

var program = new Program();
program.LaunchMinecraft();
     static void Main(string[] args)
            {
                Program pg = new Program();
                pg.LaunchMinecraft();

            }

Try this. 尝试这个。

You are trying to call an instance method (ie. a method that needs a specific object to operate on) from a static method (a method that works without a specific object). 您正尝试从静态方法(无需特定对象的方法)调用实例方法(即需要特定对象进行操作的方法)。 Make the LaunchMinecraft method static as well. 使LaunchMinecraft方法也是静态的。

我对C#了解不多,但Main()方法是静态的,而LaunchMinecraft()则不是,这就是导致此错误的原因。

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

相关问题 非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property 非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property 非静态字段,方法或属性是否需要对象引用? - An object reference is required for the non-static field, method, or property? 非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property 非静态字段,方法或属性(数据集)需要对象引用 - An object reference is required for the non-static field, method, or property (dataset) 错误:非静态字段,方法或属性需要对象引用 - Error: An object reference is required for the non-static field, method, or property 对象引用是必需的非静态字段,方法或属性错误 - An object reference is required non-static field, method, or property error 非静态字段,方法或属性需要对象引用 - Object reference is required for non-static field, method or property 非静态字段、方法或属性需要 object 引用 - An object reference is required for the non-static field, method, or property GetAbbreviatedMonthName中的非静态字段,方法或属性需要对象引用 - An object reference is required for the non-static field, method, or property in GetAbbreviatedMonthName
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM