C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c# – 无法使Ninject.Extensions.Conventions工作大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直试图让(Ninject 3)的Ninject.Extensions.Conventions工作,没有运气.我把它归结为一个找到的样本控制台应用程序,我甚至无法做到这一点.这就是我所拥有的:

class Program
    {
        static void Main(string[] args)
        {
            var kernel = new StandardKernel();
            kernel.Bind(x => x
               .FromThisAssembly()
               .SelectAllClasses()
               .BindAllInterfaces());

            var output = kernel.Get<IConsoleOutput>();
            output.HelloWorld();

            var service = kernel.Get<Service>();
            service.OutputToConsole();

            Console.ReadLine();
        }

        public interface IConsoleOutput
        {
            void HelloWorld();
        }

        public class ConsoleOutput : IConsoleOutput
        {
            public void HelloWorld()
            {
                Console.WriteLine("Hello World!");
            }
        }

        public class Service
        {
            private readonly IConsoleOutput _output;
            public Service(IConsoleOutput output)
            {
                _output = output;
            }

            public void OutputToConsole()
            {
                _output.HelloWorld();
            }
        }
    }

我还尝试了各种组合的FromAssembliesMatching,SelectAllTypes,BindDefaultInterfaces等.一切都会引发错误激活.没有匹配的绑定可用,并且该类型不可自绑定.

只是为了理智,如果我用以下方法进行手动绑定:

kernel.Bind<IConsoleOutput>().To<ConsoleOutput>();

一切正常.很明显,我只是遗漏了一些东西.

解决方法

正如山姆所说,它是由不公开的类型造成的.它们是非公共“程序”类的内部类型.

使程序公开或添加.IncludingNonPublicTypes():

kernel.Bind(x => x
    .FromThisAssembly()
    .IncludingNonPublicTypes()
    .SelectAllClasses()
    .BindAllInterfaces());

(我已经确认这两个都有效,而你的代码却没有).

这是您的官方消息来源:
https://github.com/ninject/ninject.extensions.conventions/blob/master/src/Ninject.Extensions.Conventions.Test/IntegrationTests/NonPublicTypesTests.cs

注意:在旧版本的Ninject中,此方法称为IncludeNonePublicTypes(None vs Non).

大佬总结

以上是大佬教程为你收集整理的c# – 无法使Ninject.Extensions.Conventions工作全部内容,希望文章能够帮你解决c# – 无法使Ninject.Extensions.Conventions工作所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: