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

本文关键字:构造函数 参数 方法 注入 依赖 IntPtr Ninject | 更新日期: 2023-09-27 18:35:45

我之前得到了这个问题的帮助:

Ninject:激活字符串时出错

@nemensv解决了这个问题,但我立即得到了一个关于 Intptr 的新例外。(请参阅下面的激活路径)。

一直在看这里的ctor

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:将依赖 IntPtr 注入到构造函数的参数方法中

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

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

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