我正在使用.NET制作一个控制台应用程序,当我运行这个程序时,出现了这个错误。error CS0120: An object reference is required for the non-static field, method, or property Encoding.GetBytes(string)
. 它说错误来自这一行:content.Add(Encoding.GetBytes("\n"+str));
(content是string类型的列表,str是string)
我正在使用.NET制作一个控制台应用程序,当我运行这个程序时,出现了这个错误。error CS0120: An object reference is required for the non-static field, method, or property Encoding.GetBytes(string)
. 它说错误来自这一行:content.Add(Encoding.GetBytes("\n"+str));
(content是string类型的列表,str是string)
在
Encoding.GetBytes Method
文档中:它不是静态方法,因此不允许直接调用
Encoding.GetBytes
方法。相反,您需要声明并分配一个
Encoding
变量;然后才使用这个变量来调用GetBytes
方法。要将
bytes
添加到列表的内容中,可以首先将bytes
转换为所需的格式。[不是
.ToString()
,它将返回结果:"System.Byte[]"]