PlatformNotSupportedException Ninject Xamarin.iOS

Jos*_*eph 1 c# ninject xamarin.ios

我有一个使用 Xamarin.iOS 开发的 iOS 应用程序,它使用 Ninject 3.3.0 进行 IoC。我能够毫无问题地绑定接口和实现,但是我得到了PlatformNotSupportedException解决这些绑定的IResolutionRoot.Get<T>(). 我正在连接的 Macbook 上启动模拟器。我创建了一个测试(空白)iOS 应用程序来演示该问题。以下是相关行:

[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate
{
  ...

  public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
  {
     ...

     var kernel = new StandardKernel();
     kernel.Bind<IFoo>().To<Foo>();

     var test = kernel.Get<IFoo>(); //exception thrown here

     return true;
  }
}
Run Code Online (Sandbox Code Playgroud)

这是堆栈跟踪的顶部(可以提供更多):

" 在 System.Reflection.Emit.DynamicMethod..ctor (System.String name, System.Type returnType, System...."

根据站点,内核为其绑定创建了这些 DynamicMethod。既然.Net Standard 2.0支持Ninject ,为什么我会从这么简单的操作中得到这个异常?

Jos*_*eph 6

更新: Portable.Ninject 工作,所以如果你在 iOS 上使用它。以下是我如何到达那里以及为什么常规 Ninject 不起作用的解释。

不支持System.Reflection.Emit 。这可能意味着 Ninject 的大部分内容不适用于 iOS。

这些链接对我特别有误导性:

https://developer.xamarin.com/api/type/System.Reflection.Emit.DynamicMethod/

现在已经阅读了这个限制,很明显本文中提到的生成阻碍了 Ninject 的工作,尽管没有明确说明。

http://arteksoftware.com/ioc-containers-with-xamarin/

这篇文章的作者提到了这个限制,尽管他似乎让它起作用了。

  • tl;dr:使用 Portable.Ninject (2认同)
  • 同意。我已经更新了我的答案,以使解决方案更易于访问。 (2认同)