简体   繁体   English

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

[英]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.

相关问题 我可以通过ASP.Net中的页面/控件的前端将类实例传递给自定义用户控件吗? - Can I pass a class instance to a custom user control via the front end of a page/control in ASP.Net? ASP.NET:通过Web方法获取当前URL? - ASP.NET: Getting the Current URL via a Web Method? 如何通过uri或http正文将复杂对象的实例传递给asp.net Web API控制器而没有任何参数? - How to pass instance of complex object via uri or http body without any params to asp.net web api controller? 通过AJAX使用页面方法/ Web服务进行ASP.NET本地化 - ASP.NET localization with Page Methods/Web Services via AJAX 通过c#ASP.Net中的网页运行TSQLScripts - Run TSQLScripts via the Web Page in c# ASP.Net 在ASP.NET网页之间传递值 - Pass value between asp.net web page 在 Page_Load 方法中获取真正当前的 class 名称 - ASP.NET Web 应用程序(.NET Framework) - Get really current class name in Page_Load method - ASP.NET Web Application (.NET Framework) ASP.NET Web API 2 通过使用 Autofac 的依赖注入访问当前用户声明 - ASP.NET Web API 2 access current user claims via dependency injection with Autofac ASP.NET 菜单中的当前页面样式 - Styling current page in ASP.NET menu 重定向到ASP.Net中的当前页面 - redirect to current page in ASP.Net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM