FailedtoPassGenericParametersThroughCastleWindsor在尝试使用CastleWindsor创建参数化实例时,传递泛型参数似乎有问题Failedtopassthe一般参数的演示privatestaticvoidMain(string[]args){PassGenericParamAtResolutionTime();控制台.ReadLine();}privatestaticvoidPassGenericParamAtResolutionTime(){Console.WriteLine("传递通用参数失败");varcontainer=newWindsorContainer();container.Register(Component.For().ImplementedBy());varruntimeConstructorParam=newGenericManager("这个Id打不通");varruntimeArguments=newArguments(新对象[]{runtimeConstructorParam});varshouldBeParameterizedCoordinator=container.Resolve(runtimeArguments);Console.WriteLine(shouldBeParameterizedCoordinator.Log);控制台输出PassinggenericargumentfailsBirthfromparameterlessconstructor,whichshouldnothappen如果我注释掉下面的无参数构造函数,我会得到以下异常:Castle.MicroKernel.Resolvers。DependencyResolverException未处理缺少依赖项。组件Sand.SandCoordinator`1[[Sand.Simpleton,WindsorSand,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]]依赖于Sand.IGenericManager`1[Sand.Simpleton],无法解决。确保依赖项作为服务在容器中正确注册,或作为内联参数提供。DemoclassSandCoordinator:ISandCoordinatorwhereTSimpleton:ISimpleton{publicSandCoordinator(){Log="从无参数构造函数中诞生,这不应该发生";}publicSandCoordinator(IGenericManagermanager){Log="从参数化构造函数中诞生";Log+=Environment.NewLine+"BornWithManager:"+manager.Id;}公共字符串日志{得到;私有集;解决方案/解决方法?[使用Castle.Core.dll和Castle.Windsor.dll3.1.0(2012-08-05)]你的SandCoordinator依赖于IGenericManager,而不是GenericManager当您将一个值放入Arguments中并希望Windsor将其用于其具体类型以外的其他内容时,您必须指定它。以上是C#学习教程:我通过CastleWindsor传递通用参数共享的全部内容失败。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——newArguments{{typeof(IGenericManager),runtimeConstructorParam}};本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: