如何覆盖或扩展.net主类.例如
public class String
{
public boolean contains(string str,boolean IgnoreCase){...}
public string replace(string str,string str2,boolean IgnoreCase){...}
}
Run Code Online (Sandbox Code Playgroud)
后
string aa="this is a Sample";
if(aa.contains("sample",false))
{...}
Run Code Online (Sandbox Code Playgroud)
可能吗?
Jar*_*Par 22
String类是密封的,因此您无法从中继承.扩展方法是您最好的选择.它们与实例方法具有相同的感觉,而没有继承成本.
public static class Extensions {
public static bool contains(this string source, bool ignoreCase) {... }
}
void Example {
string str = "aoeeuAOEU";
if ( str.contains("a", true) ) { ... }
}
Run Code Online (Sandbox Code Playgroud)
您需要使用VS 2008才能使用扩展方法.