2012-06-25 71 views
0

嘲笑c#中静态类的最佳方法是什么?例如:使用Rhino模仿c#中的静态方法模拟

String appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath; 

我想嘲笑这一点,但将需要一个接口,它包装在一个类中,什么是最好的方法,并做这种工作方式为所有静态方法?

这种方法不会造成很多最无意义的接口和类吗?

回答

6

什么是用C#

嘲讽了一个静态类的最佳方法,最好的方法是隐藏背后的抽象那些静态调用。那些可以是假的。例如:

public interface IApplicationPhysicalPathProvider 
{ 
    string ApplicationPhysicalPath { get; } 
} 

public class AspNetApplicationPhysicalPathProvider : IApplicationPhysicalPathProvider 
{ 
    public string ApplicationPhysicalPath 
    { 
     get { return HostingEnvironment.ApplicationPhysicalPath; } 
    } 
} 

此建议适用于任何模拟框架,即使您根本不使用模拟框架。

相关问题