bug*_*net 2 c# extending-classes
在javascript中扩展核心类很容易.我觉得它在C#中并不那么容易.我想在String类中添加一些内容,以便我可以执行以下操作:
string s = "the cat's mat sat";
string sql = s.smartsingleQuote();
Run Code Online (Sandbox Code Playgroud)
因此给了我
the cat''s mat sat
Run Code Online (Sandbox Code Playgroud)
这是否可行,还是我必须为此编写一个函数?
Anu*_*raj 12
是的,可以使用扩展方法 - MSDN
这是一个示例代码.
public static class Extns
{
public static string smartsingleQuote(this string s)
{
return s.Replace("'","''");
}
}
Run Code Online (Sandbox Code Playgroud)
免责声明:未经测试.
| 归档时间: |
|
| 查看次数: |
251 次 |
| 最近记录: |