Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想向 mscorlib 添加一些方法。例如:
字符串 abc;
abc.IsNumeric()
我希望能解释我的问题。
您不能将方法添加到 mscorlib,但是您可以使用扩展方法,使它们看起来好像是在字符串上定义的,例如
public static class StringExtensions { public static bool IsNumeric(this string s) { // TODO } }
然后你可以根据你的要求打电话,例如
"1234".IsNumeric()
格雷格给了你一个很好的答案。只是想补充一点,您可以在此处阅读有关扩展方法的更多信息: