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

编程入门 行业动态 更新时间:2024-10-19 12:36:39
本文介绍了Ninject:将依赖IntPtr注入构造函数的参数方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这个问题我早些得到帮助:

I got help earlier with this question:

Ninject:激活字符串时出错

@nemensv解决了一个问题,但是我立即获得了有关IntPtr的新例外. (请参见下面的激活路径).

@nemensv solved that one but I immediatley got a new exception regarding IntPtr. (see activation path below).

开始浏览ctor 此处

此行中只有IntPtr显示的地方:

Only place IntPtr shows up is in this line:

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

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

How can I solve this using 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中的代码:

My code in 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+SerializeDelegate,因此无法创建BlobSerializerDelegate.您可能需要告诉ninject如何创建BlobSerializerDelegate-甚至有必要-或告诉它如何实例化SQLiteConnection. 我认为对于给定的情景,ToMethod绑定将是最佳选择:

As ninject states it can't created an BlobSerializerDelegate because it doesn't know how to instanciated a BlobSerializerDelegate+SerializeDelegate. You either need to tell ninject how to created BlobSerializerDelegate - if that's even necessary - or tell it how to instanciate the SQLiteConnection. I think for the given situtation the ToMethod binding would be best:

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

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

The ToMethod action should just new the SQLiteConnection as you would without a DI container.

更多推荐

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

本文发布于:2023-11-12 12:13:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581471.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   参数   方法   Ninject   IntPtr

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!