C#开发Executor遇两类错误:api未定义、命名空间成员违规
编译错误解决方案:"当前上下文中不存在名称‘api’"与"命名空间不能直接包含字段、方法或语句这类成员"
错误原因及修复方案:
错误1:命名空间不能直接包含字段、方法或语句这类成员
你将ExploitAPI api = new ExploitAPI();直接定义在了命名空间ZynTap_Executor下,C#中命名空间仅允许包含类、接口、枚举等类型成员,不能直接放置字段实例化代码。错误2:当前上下文中不存在名称‘api’
原代码中api的定义不在Form1类的作用域内,导致类内的按钮点击方法无法访问这个变量。
修正后的完整代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using WeAreDevs_API; namespace ZynTap_Executor { public partial class Form1 : Form { // 将api实例移到Form1类内部作为字段 ExploitAPI api = new ExploitAPI(); public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Environment.Exit(0); } Point lastPoint; private void panel1_MouseDown(object sender, MouseEventArgs e) { lastPoint = new Point(e.X, e.Y); } private void panel1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { this.Left += e.X - lastPoint.X; this.Top += e.Y - lastPoint.Y; } } private void button2_Click(object sender, EventArgs e) { WindowState = FormWindowState.Minimized; } private void button4_Click(object sender, EventArgs e) { fastColoredTextBox1.Clear(); } private void Form1_Load(object sender, EventArgs e) { } private void button3_Click(object sender, EventArgs e) { api.SendLuaScript(fastColoredTextBox1.Text); } private void button8_Click(object sender, EventArgs e) { api.LaunchExploit(); } private void button5_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog1 = new OpenFileDialog(); if (openFileDialog1.ShowDialog() == DialogResult.OK) { openFileDialog1.Title = "Open"; fastColoredTextBox1.Text = File.ReadAllText(openFileDialog1.FileName); } } private void button6_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); if (saveFileDialog1.ShowDialog() == DialogResult.OK) { using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew)) using (StreamWriter sw = new StreamWriter(s)) { sw.Write(fastColoredTextBox1.Text); } } } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { fastColoredTextBox1.Text = File.ReadAllText($"./Scripts/{listBox1.SelectedItem}"); } private void button7_Click(object sender, EventArgs e) { listBox1.Items.Clear();//Clear Items in the LuaScriptList Functions.PopulateListBox(listBox1, "./Scripts", "*.txt"); Functions.PopulateListBox(listBox1, "./Scripts", "*.lua"); } } }
内容的提问来源于stack exchange,提问作者WoidZ
相关产品推荐
相关产品推荐

