我有一个扩展方法:
namespace CustomExtensions
{
//Extension methods must be defined in a static class
public static class StringExtension
{
// This is the extension method.
// The first parameter takes the "this" modifier
// and specifies the type for which the method is defined.
public static string TrimAndReduce(this string str)
{
return ConvertWhitespacesToSingleSpaces(str).Trim();
}
public static string ConvertWhitespacesToSingleSpaces(this string value)
{
return Regex.Replace(value, @"\s+", " ");
}
}
}
我通过以下方式致电
:using CustomExtensions;
string text = " I'm wearing the cheese. It isn't wearing me! ";
text = text.TrimAndReduce();
我想创建一个文本字段,并允许用户以完全相同的方式调用我的方法。用户将输入:
string text = " I'm wearing the cheese. It isn't wearing me! ";
text = text.TrimAndReduce();
我有什么选择?我不能使用第三方库或反射,主要是因为我想自己构建一个解决方案。