簡體   English   中英

通過InjectionConstructor傳遞asp.net網頁的當前實例

[英]pass current instance of asp.net web page via InjectionConstructor

public class LogUtil : ILogUtility
{
   ... 
   public LogUtil(System.Type classType) 
   ....
}

從后面的WebForm1.aspx頁面代碼,PageLoad事件中,我能夠成功執行以下操作。

LogUtil logger = new LogUtil(this.GetType());

但是,當我嘗試從WebForm1.aspx的以下代碼進行pageload事件時。

var container = new UnityContainer();
System.Type type = this.GetType();

container.RegisterType<ILogUtility, LogUtil>(new InjectionConstructor(this.GetType()));  <--Error

在上面的行中,出現以下錯誤...

LogUtil類型沒有使用參數(WebForm1)的構造函數。

我究竟做錯了什么? 如何通過InjectionConstructor傳入當前的類實例? 為什么我能夠成功地將this.GetType()成功地直接傳遞到LogUtil構造函數中,但是我有能力通過InjectionConstructor做到這一點?

在Logger類上,您的類型是Logger類,因此可以正常工作。 它不適用於Webform,因為this.GetType()返回Webform類型。

我可以通過更新類的構造函數簽名來修復它,如下所示:

public class LogUtil : ILogUtility
{
   ... 
   public LogUtil(object classType)  
   ....
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM