Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在模块中使用容器解析?_C#_Inversion Of Control_Ioc Container_Autofac - Fatal编程技术网

C# 如何在模块中使用容器解析?

C# 如何在模块中使用容器解析?,c#,inversion-of-control,ioc-container,autofac,C#,Inversion Of Control,Ioc Container,Autofac,我是Autofac的初学者。 有人知道如何使用模块中的container.Resolve吗 public class MyClass { public bool Test(Type type) { if( type.Name.Begin("My") ) return true; return false; } } public class MyModule1 : Autofac.Module { protected override

我是Autofac的初学者。 有人知道如何使用模块中的container.Resolve吗

public class MyClass
{
  public bool Test(Type type)
    {
       if( type.Name.Begin("My") )  return true;
         return false;
    }
}

public class MyModule1 : Autofac.Module
{
    protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration)
    {
        var type = registration.Activator.LimitType;
        MyClass my = container.Resolve<MyClass>();  //How to do it in Module? 
        my.Test(type);
        ...
    }
}
公共类MyClass
{
公共布尔测试(类型)
{
if(type.Name.Begin(“My”))返回true;
返回false;
}
}
公共类MyModule1:Autofac.Module
{
受保护的覆盖无效AttachToComponentRegistration(IComponentRegistry ComponentRegistration,IComponentRegistration注册)
{
变量类型=registration.Activator.LimitType;
MyClass my=container.Resolve();//如何在模块中执行此操作?
我的测试(类型);
...
}
}

如何获取模块中的容器?

您无法从模块中的容器解析组件。但您可以单独附加到每个组件解析事件。所以当你们遇到有趣的组件时,你们可以用它做任何事情。可以说,从概念上讲,您可以解析组件

public class MyModule : Autofac.Module
{
    protected override void AttachToComponentRegistration(IComponentRegistry registry, IComponentRegistration registration)
    {
        registration.Activated += (sender, args) =>
        {
            var my = args.Instance as MyClass;
            if (my == null) return;

            var type = args.Component.Activator.LimitType;
            my.Test(type);
            ...
        };
    }
}

您可以使用IActivatingEventArgs上下文属性:

protected override void AttachToComponentRegistration(IComponentRegistry registry, IComponentRegistration registration)
    {
        registration.Activated += (sender, args) =>
        {
            args.Context.Resolve<...>();
            ...
        };
    }
受保护的覆盖无效AttachToComponentRegistration(IComponentRegistry注册表,IComponentRegistration注册)
{
注册。已激活+=(发件人,参数)=>
{
args.Context.Resolve();
...
};
}

您想要实现什么目标?为什么在
附件组件注册中需要此项?因为当调用
AttachToComponentRegistration
时,Autofac仍处于容器构建“阶段”,因此您无法使用同一容器从中解析类型……我同意@nemesv。如果您想在构建阶段解决某些问题,那么您就做错了。请允许我们通过解释您试图实现的目标和原因,为您提供反馈。Autofac的目的是提供动态代理。我想在构建阶段解决一些问题,因为我希望构建阶段的每一个代码都能用resolve方法动态调用一些类。(越来越有活力)我的想法错了?国际奥委会本身就不能动态的吗?