我正在使用ASP.NET MVC
和Ninject
,我試圖創建一個ActionResult
工廠。比方說,我有以下ActionResults
:如何創建一個返回具有不同參數的不同實例的工廠?
public class SuccessResult : ActionResult
{
public string SuccessMessage { get; set; }
public SuccessResult(string successMessage) { ... }
}
public class FailResult : ActionResult
{
public int FailCode { get; set; }
public FailResult(int failCode) { ... }
}
public class DataResult : ActionResult
{
public object Data { get; set; }
public string MimeType { get; set; }
public DataResult(object dataToSerialize, string mimeType) { ... }
}
所以每個ActionResult
,參數的參數類型和數量會有所不同。我創建了一個ActionResultFactory
看起來像這樣:
public class ActionResultFactory
{
private readonly IKernel _kernel;
public ActionResultFactory(IKernel kernel)
{
_kernel = kernel;
}
public T Create<T>() where T : ActionResult
{
return _kernel.Get<T>(); // how do I pass the parameters?
}
}
我怎麼會寫這個工廠,以便它可以帶參數,並將其傳遞給對象的構造?或者我應該做這樣的代替:
var result = factory.Create<SuccessResult>();
result.SuccessMessage = "Success!";
var result = factory.Create<FailResult>();
result.FailCode = 404;
var result = factory.Create<DataResult>();
result.Data = file;
result.MimeType = "text/plain";
其中每個屬性被公開曝光和我對象創建後,將它們分配?
你的參數在哪裏?你的意思是`T`? – 2010-11-23 03:41:32