![](/img/trans.png)
[英]member <method> cannot be accessed with an instance reference; qualify it with a type name instead
[英]Member '<method>' cannot be accessed with an instance reference
整个错误文本是:
使用实例引用无法访问成员
'System.Text.RegularExpressions.Regex.Replace(string, string, string, System.Text.RegularExpressions.RegexOptions)'
; 用类型名称来限定它
这是代码。 我在这里的另一篇文章中删除了“静态”,但它仍然给我错误。
我很感谢所有专家的帮助 - 谢谢!
public string cleanText(string DirtyString, string Mappath)
{
ArrayList BadWordList = new ArrayList();
BadWordList = BadWordBuilder(BadWordList, Mappath);
Regex r = default(Regex);
string element = null;
string output = null;
foreach (string element_loopVariable in BadWordList)
{
element = element_loopVariable;
//r = New Regex("\b" & element)
DirtyString = r.Replace(DirtyString, "\\b" + element, "*****", RegexOptions.IgnoreCase);
}
return DirtyString;
}
问题在于使用方法Replace
而不是在声明中使用static。 您需要使用typename Regex
而不是变量r
DirtyString = Regex.Replace(DirtyString, "\\b" + element, "*****", RegexOptions.IgnoreCase);
原因是在C#中你不能通过类型的实例访问static
方法。 这里Replace
是static
因此必须通过Regex
类型使用
好的,首先; default(Regex)
将只返回null,因为Regex
是一个引用类型。 因此,即使您的代码已编译,它肯定会在此行中因NullReferenceException
崩溃,因为您从未向r
分配任何有效内容。
DirtyString = r.Replace(DirtyString, "\\b" + element, "*****", RegexOptions.IgnoreCase);
接下来,编译器会告诉您确切的问题所在; Replace
是一个静态方法,而不是实例方法,因此您需要使用typename而不是实例变量。
DirtyString = Regex.Replace(...);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.