假设你有一个类型SomeImplementation,它显示的实现了ISomeInterface接口的方法SomeMethod(),那么如何才能取到这个SomeImplementation.SomeMethod()的MethodInfo呢?
一些程序员使用下面的方法来获取MethodInfo:
MethodInfo mi = typeof(SomeImplementation).GetMethod(
"VBLib.ISomeInterface.SomeMethod",
BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
但是有些时候这样做是错误的,它并不能取得方法的MethodInfo。原因是上面代码使用的私有方法的名字是编译器在编译代码时使用的实现细节。C#语言开发的代码可以使用这种方法获取到,但如果是其它语言(如,VB.NET)就不一定能成功了。
试一下对下面的代码,在C#中使用上面的代码看是否能取到MethodInfo。
Public Interface ISomeInterface
Function SomeMethod() As String
End Interface
Public Class SomeImplementation
Implements ISomeInterface
Private Function SomeMethod() As String Implements ISomeInterface.SomeMethod
Return "hello"
End Function
End Class
你会发现对于上面用VB.NET开发的代码,你无法使用前面的代码取到MethodInfo.
正确的方法
下面是取得MethodInfo的正确方法:
MethodInfo imi = typeof(ISomeInterface).GetMethod("SomeMethod");
InterfaceMapping map = typeof(SomeImplementation).GetInterfaceMap(
typeof(ISomeInterface));
int index = Array.IndexOf(map.InterfaceMethods, imi);
MethodInfo result = map.TargetMethods[index];
Colin Mackay讲述mock的论文 中列出了一些常见的使用mock的场景:
- 真实对象有着不确定的行为
- 真实对象很难创建
- 真实对象的行为很难触发
- 真实对象响应缓慢
- 真实对象是用户界面
- 真实对象使用了回调机制
- 真实对象尚未存在
K Scott Allen 在
Mocks - It's A Question Of When 认为,即使是上面列表中的内容也稍显肤浅,从更深刻、更普遍的意义而言,应该是”在你想把被测试的代码分离的时候,
martin fowler 的
test doubles[mocks]就显出了作用。“简而言之,按照Allen的观点,用了mock对象,业务组件的测试代码就可以不依赖其它组件了;试举一例来看,A依赖于B,但A的单元测试只会因为A的问题而出现问题,跟B的状态无干。
"
Mock Roles, Not Objects" 的作者们写到,mock技术:
"……在基于对象角色构建的系统中,可以识别出各种角色类型……尤其是,我们已经知道了Mock Object可以为我们带来的最大好处——曾被称为接口识别的概念"。
今天试着将
Team Foundation Server 2008装在Windows Server 2008 Enterprise RTM上。在TFS 2008 进行“系统状况检查”时失败,提示SQL全文搜索服务未安装。因为SQL全文搜索已经无缝集成到了
SQL Server 2008 CTP February 2008的数据库引擎中,所以在我安装了带有全文搜索功能的
SQL Server 2008 CTP February 2008后,在Windows服务列表中也是找不到
SQL Server FullText Search服务的。为了解决这个问题我在网上开始查找解决方案,最后在这里找到了。呵呵,很不幸,目前TFS 2008还无法和SQL 2008一起工作。
解决办法有两个:
1、等到TFS 2008 SP1发布后再装
2、使用SQL Server 2005
呵呵,我选择了后者。希望对大家有帮助,毕竟装Team Foundation Server是很耗时的事儿。
下面是Team Foundation Server 2008系统检测的结果:
The System Health Check has detected a problem that will cause Setup to fail.
Description
The SQL Full Text Search service is not installed.
Workaround / Remedy
The SQL Full Text Search service is not installed. To install this service, you must run Microsoft SQL Server setup and install this component.
More information
For additional information and help please refer to: http://go.microsoft.com/fwlink/?LinkId=79226
The System Health Check has detected a problem that will cause Setup to fail.
Description
The SQL Full Text Search service is stopped.
Workaround / Remedy
The SQL Full Text Search service is in a stopped state. In Control Panel, double-click Administrative Tools, double-click Services, and then look for the service. If the service is not running, start it by right-clicking the service and then clicking Start.
More information
For additional information and help please refer to: http://go.microsoft.com/fwlink/?LinkId=79226
The System Health Check has detected a problem that will cause Setup to fail.
Description
The SQL Full Text Search service is not configured to start automatically.
Workaround / Remedy
The SQL Full Text Search service is not configured to start automatically when the computer starts. In Control Panel, double-click Administrative Tools, double-click Services, and then look for the service. Double-click the service to view the service properties. In the Startup type list, click Automatic, and then click OK.
More information
For additional information and help please refer to: http://go.microsoft.com/fwlink/?LinkId=79226
摘要: 今天读了 Steve Yegge的Code's Worst Enemy 触动了我,将自己的看法也记录下来。 Steve Yegge 说“......I believe, quite staunchly I might add, that the worst thing that can happen to a code base is size.”。 我觉得庞大的代码库是极...
阅读全文
摘要: 好久没有写东西了,今天将以前给同事写的一段代码改了一下共享出来。这段代码用于实现在点击按钮后禁用它直到操作完成。有更好的方法大家可以讨论!Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><%@PageLanguage="C#"%><!...
阅读全文
摘要: ASP.NET 2.0 中增加了一个 IButtonControl 接口(Button、ImageButton、LinkButton控件实现了此接口),在这个接口中定义了一个 PostBackUrl 属性,使用这个属性可以实现在用户点击按钮时将页面的内容回发到另外一个页面。并且在另一个页面中可以使用 Page.PreviousPage 属性获取到前一个页面的实例,通过 PreviousPage.F...
阅读全文