2012-06-25 47 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; } 
    } 
} 

此建議適用於任何模擬框架,即使您根本不使用模擬框架。

相關問題