Ninject:将依赖IntPtr注入到构造函数的参数方法中

Ninject: Injection of dependency IntPtr into parameter method of constructor

我早些时候得到了这个问题的帮助:

Ninject: Error Activating Strings

@nemensv 解决了那个问题,但我立即得到了一个关于 IntPtr 的新异常。 (见下面的激活路径)。

一直在浏览 ctor here

唯一出现 IntPtr 的地方是在这一行:

 Result r = Platform.SQLiteApi.Open(databasePathAsBytes, out handle, (int)openFlags, IntPtr.Zero);

如何使用 ninject 解决这个问题?

完全异常:

Activation path:

  6) Injection of dependency IntPtr into parameter method of constructor

  5) Injection of dependency BlobSerializerDelegate+SerializeDelegate into parameter serializeDelegate of constructor

  4) Injection of dependency IBlobSerializer into parameter serializer of constructor

  3) Injection of dependency SQLiteConnection into parameter connection of constructor

  2) Injection of dependency ICarRepository into parameter carRepository of constructor

  1) Request for MainViewModel

我在 Ninject 中的代码:

 Bind<SQLiteConnection>()
                .ToSelf()
                .WithConstructorArgument("databasePath", path);
            Bind<ISQLitePlatform>().To<SQLitePlatformWinRT>();
            Bind<IBlobSerializer>().To<BlobSerializerDelegate>();

谢谢!

编辑:

这对我有用:

Bind<SQLiteConnection>().ToMethod(ctx => new SQLiteConnection(new SQLitePlatformWP8(), path));

正如 ninject 所述,它无法创建 BlobSerializerDelegate,因为它不知道如何实例化 BlobSerializerDelegate+SerializeDelegate。您要么需要告诉 ninject 如何创建 BlobSerializerDelegate - 如果有必要 - 或者告诉它如何实例化 SQLiteConnection。 我认为对于给定的情况,ToMethod 绑定是最好的:

Bind<SQLiteConnection>().ToMethod(ctx => new SQLiteConnection(...));

ToMethod 操作应该只是 new SQLiteConnection,就像没有 DI 容器一样。