静态和扩展方法策略
我正在为我的项目开发一个个人助手库。 我的库有很多辅助类和扩展方法,但我无法决定该辅助类的命名空间和方法是静态还是扩展?
在什么情况下方法应该是静态的? StringUtility.ToBase64("xxxx")
在什么情况下,方法应该是扩展静态的? (我知道扩展必须是静态的) "xxxx".ToBase64()
我如何轻松确定这种行为的策略?
I'm working on a personal helper library for my projects.
I have a lot of helper class and extension methods for my library but I can not decide this helper class namespaces and method to static or extension?
In which case the methods should be static? StringUtility.ToBase64("xxxx")
In which case the methods should be extension-static? (I know extensions must be static) "xxxx".ToBase64()
How can i determine strategy easily for this behaviours?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的决定如下:“此方法是否对其扩展的对象执行某些操作,返回某些内容并且不会更改扩展的对象”,然后我使用扩展方法。当这些辅助函数执行其他处理对象类型的操作时,我使用静态类方法。我希望这对你有一点帮助。
My decision is following: "Is this method doing something with the object it is extending, returning somethind and doesn't change the extended object" then I use extended methods. I use static class methods when those helper functions do some other which deals with the type of object. I hope that helps you a bit.
http://blogs .msdn.com/b/brada/archive/2009/01/12/framework-design-guidelines-extension-methods.aspx
http://blogs.msdn.com/b/brada/archive/2009/01/12/framework-design-guidelines-extension-methods.aspx