简体   繁体   中英

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..

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

But when I attempt the following code from WebForm1.aspx, on pageload event..

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).

What am I doing wrong? How can I pass in the current instance of class via InjectionConstructor? Why am I able to pass in this.GetType() successfully directly into LogUtil constructor but I am uanble to do it via InjectionConstructor???

On your Logger class, your type is the Logger class, so it works. It doesn't work on the webform, because this.GetType() is returning the type 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)  
   ....
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM