How to use the `Ninject.Extensions.Factory` to dynamically generate factories for internals classes?

382 views
Skip to first unread message

Frank Abel Cancio Bello

unread,
Feb 6, 2015, 8:21:40 PM2/6/15
to castle-pro...@googlegroups.com
I need to use the `Ninject.Extensions.Factory` to generate the constructor of internal classes. Follow one example:

    using Ninject.Extensions.Conventions;
    using Ninject.Modules;
    using Ninject.Extensions.Factory;
    
    namespace ClassLibrary
    {
        using System;
    
        namespace ClassLibrary
        {
            internal class Class1
            {
                public void Print(string message)
                {
                    Console.WriteLine(message);
                }
            }
    
            internal interface IClass1Factory
            {
                Class1 Create();
            }
    
            public interface IInterface2
            {
                void PrintMessage();
            }
            internal class Class2 : IInterface2
            {
                private readonly IClass1Factory _class1Factory;
    
                public Class2(IClass1Factory class1Factory)
                {
                    _class1Factory = class1Factory;
                }
    
                public void PrintMessage()
                {
                    Class1 class1 = _class1Factory.Create();
                    class1.Print("Class2's IInterface2 'PrintMessage' implementation.");
                }
            }
    
            public class MyNinjectModule : NinjectModule
            {
                public override void Load()
                {
                    Kernel.Bind(r => r
                    .FromThisAssembly()
                    .IncludingNonePublicTypes()
                    .SelectAllClasses()
                    .BindAllInterfaces());
                    
                    Kernel.Bind<IClass1Factory>().ToFactory();
                }
            }
        }
    }


Application using the library:

    using ClassLibrary.ClassLibrary;
    using Ninject;
    
    namespace ConsoleApplication3
    {
        class Program
        {
            static void Main(string[] args)
            {
                IKernel kernel = new StandardKernel();
                kernel.Load<MyNinjectModule>();
                IInterface2 interface2 = kernel.Get<IInterface2>();
                interface2.PrintMessage();
            }
        }
    }

Even after include `[assembly: InternalsVisibleTo(InternalsVisible.ToDynamicProxyGenAssembly2)]` I'm getting the following run tine error:

> {"Type 'Castle.Proxies.IClass1FactoryProxy' from assembly
> 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral,
> PublicKeyToken=null' is attempting to implement an inaccessible
> interface.":""}

Any idea how to fix this?

Frank Abel Cancio Bello

unread,
Feb 10, 2015, 9:48:03 AM2/10/15
to castle-pro...@googlegroups.com
Just for the record: Solution here.
Reply all
Reply to author
Forward
0 new messages