首页 > 解决方案 > PropertiesAutowired 不适用于 Autofac 中的 RegisterGeneric

问题描述

我有一个没有泛型接口的泛型类,我尝试将其注册为

     builder.RegisterGeneric(typeof(ValidationHelperBase<>)).PropertiesAutowired();

但是 PropertiesAutowired 在控制器中不起作用(会得到一个对象空引用异常),我只能像这样手动解决它

  var instance = DependencyResolver.Current.GetService<ValidationHelperBase<Student>>();

我用谷歌搜索了这个问题,只找到了这样的东西Registering Generic Interfaces and classes with Autofac,问题是我的泛型类没有继承接口,只是一个普通的泛型类

 public class ValidationHelperBase<T> where T : CustomTableBase
    {
        public virtual void CheckExist(int customTableItemId)
        {
            //do logic check, and throw exception when check failed
        }
    }

标签: autofac

解决方案


推荐阅读