![](/img/trans.png)
[英]Can I pass a class instance to a custom user control via the front end of a page/control in 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.