通过 InjectionConstructor 传递 asp.net 网页的当前实例

本文关键字:实例 网页 net InjectionConstructor 传递 asp 通过 | 更新日期: 2023-09-27 18:30:49

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

从我的 WebForm1.aspx 页面代码隐藏,PageLoad 事件,我能够成功地执行以下操作。

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

但是当我尝试从 WebForm1.aspx 使用以下代码时,在页面加载事件上。

var container = new UnityContainer();
System.Type type = this.GetType();
container.RegisterType<ILogUtility, LogUtil>(new InjectionConstructor(this.GetType()));  <--Error

在上面的行,我收到以下错误...

类型LogUtil没有接受参数的构造函数(WebForm1)。

我做错了什么?如何通过 InjectionConstructor 传入类的当前实例?为什么我能通过这个。GetType() 成功地直接进入 LogUtil 构造函数,但我不愿意通过 InjectionConstructor 来做到这一点???

通过 InjectionConstructor 传递 asp.net 网页的当前实例

在 Logger 类

上,您的类型是 Logger 类,因此它可以工作。它不适用于网络表单,因为这个。GetType() 返回类型网络表单。

我能够通过更新我的类的构造函数签名来修复它,如下所示:

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