[英]pass current instance of asp.net web page via InjectionConstructor
public class LogUtil : ILogUtility
{
...
public LogUtil(System.Type classType)
....
}
From my WebForm1.aspx page code behind, PageLoad event, I am able to do the following successfully.. 从后面的WebForm1.aspx页面代码,PageLoad事件中,我能够成功执行以下操作。
LogUtil logger = new LogUtil(this.GetType());
But when I attempt the following code from WebForm1.aspx, on pageload event.. 但是,当我尝试从WebForm1.aspx的以下代码进行pageload事件时。
var container = new UnityContainer();
System.Type type = this.GetType();
container.RegisterType<ILogUtility, LogUtil>(new InjectionConstructor(this.GetType())); <--Error
at the above line, I get the following error... 在上面的行中,出现以下错误...
The type LogUtil does not have a constructor that takes the parameters (WebForm1). LogUtil类型没有使用参数(WebForm1)的构造函数。
What am I doing wrong? 我究竟做错了什么? How can I pass in the current instance of class via InjectionConstructor? 如何通过InjectionConstructor传入当前的类实例? Why am I able to pass in this.GetType() successfully directly into LogUtil constructor but I am uanble to do it via InjectionConstructor??? 为什么我能够成功地将this.GetType()成功地直接传递到LogUtil构造函数中,但是我有能力通过InjectionConstructor做到这一点?
On your Logger class, your type is the Logger class, so it works. 在Logger类上,您的类型是Logger类,因此可以正常工作。 It doesn't work on the webform, because this.GetType() is returning the type webform. 它不适用于Webform,因为this.GetType()返回Webform类型。
I was able to fix it by updating the constructor signature of my class as follows: 我可以通过更新类的构造函数签名来修复它,如下所示:
public class LogUtil : ILogUtility
{
...
public LogUtil(object classType)
....
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.