需要对象引用才能访问非静态字段、方法或属性。

4
using System;
using System.Diagnostics;
using System.Reflection.Emit;
using System.Threading;
using EasyExploits;

namespace ConsoleApp1
{
    class Program
    {
        EasyExploits.Module module = new EasyExploits.Module();

        static void Main(string[] args)
        {
            Module.LaunchExploit();
            Console.ForegroundColor = ConsoleColor.Green;
            Label:
            Console.WriteLine("Please Type 'Inject'");
            string proccess1 = Console.ReadLine();
            if (proccess1 == "Inject")
            {
                Console.WriteLine("");
                Console.WriteLine("Injected!");
                goto Begin;
            }
            else
            {
                goto Label;
            }
            Begin:
            Console.WriteLine("");
            Console.WriteLine("Enter a script and press enter to execute it.");
            string answer = Console.ReadLine();
            Module.ExecuteScript(answer);
            goto Begin





        }
    }
}

我试图解决这个问题,但无法找到解决方案,于是来到Stack Overflow。总之,我的控制台应用程序应在输入中粘贴脚本时注入EasyExploits.DLL并执行Lua脚本。然而,我收到了一个错误消息,其中包含"Module.LaunchExploit()""Module.ExecuteScript(string)"的非静态字段、方法或属性需要对象引用。由于我是C#的初学者,不太理解这个错误,如果有人能以初学者友好的简单步骤指导我,那将是太好了。


1
这个回答解决了你的问题吗?CS0120:非静态字段、方法或属性“foo”需要对象引用 - Johnathan Barclay
EasyExploits.Module模块需要在Main()方法中。 - Kishan Vaishnav
2个回答

5

你的Main方法是静态的,你只能从一个静态方法中访问同一类的静态成员。为了使你的EasyExploits.Module也是静态的,你需要做的就是:

private static readonly EasyExploits.Module module = new EasyExploits.Module();

2
然后使用该成员“module”而不是类“Module”。即使用“module.LaunchExploit();”而不是“Module.LaunchExploit();”。 - Corak
1
同样适用于ExecuteScript - Peter Csala

2
这里有两个问题:
  1. Module是一个类,但是LaunchExploit()不是静态的,也就是说需要从一个对象来运行。你可能想使用对象module。
  2. 如果您想在静态函数main中使用它,module也应该是静态的。
我还想提一下一个次要的问题:goto很少是一个好主意。如果您:
  1. 创建一个静态函数begin,在任何您使用“go to Begin”的地方都可以这样做:
begin()
return;
  1. 创建一个 static 函数 label,对它执行相同的操作

这将使您的代码更易读和调试。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接