无法从程序集加载类型,依赖注入
Cannot load type from assembly, dependency injection
这段完全相同的代码在另一个解决方案中可以正常工作,但在这个解决方案中却不行,所以我怀疑这是一个配置问题。
我的解决办法是:
-XamForms PCL
- Android 项目
- PCL 库(带有 INativeMethods)
- Android 库(使用 NativeMethods 实现 INativeMethods)
这是我在 PCL 库中的界面:
namespace DreamzLibs
{
public interface INativeMethods
{
}
}
这是 Android 库中的实现:
[assembly: Dependency(typeof(NativeMethods))]
namespace DreamzLibs.Droid
{
public class NativeMethods : Java.Lang.Object, INativeMethods
{
}
}
这是请求的地方:
public static Lazy<INativeMethods> NativeMethods = new Lazy<INativeMethods>(() => DependencyService.Get<INativeMethods> ());
这是我得到的错误:
[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.TypeInitializationException: An exception was thrown by the type initializer for Boats.App ---> System.TypeLoadException: Could not load type 'DreamzLibs.Droid.NativeMethods' from assembly 'DreamzLibs.Droid, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
[MonoDroid] at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00023] in <filename unknown>:0
[MonoDroid] at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x000af] in <filename unknown>:0
[MonoDroid] at System.Activator.CreateInstance (System.Type type) [0x00000] in <filename unknown>:0
[MonoDroid] at Xamarin.Forms.DependencyService.Get[INativeMethods] (DependencyFetchTarget fetchTarget) [0x00076] in <filename unknown>:0
[MonoDroid] at DreamzLibs.Utils.<.cctor>b__2 () [0x00000] in e:\Code\Boats\DreamzLibs\Utils.cs:25
[MonoDroid] at System.Lazy`1[DreamzLibs.INativeMethods].CreateValue () [0x00075] in <filename unknown>:0
[MonoDroid] --- End of inner exception stack trace ---
[MonoDroid] at Boats.Droid.MainActivity.OnCreate (Android.OS.Bundle) [0x0005d] in e:\Code\Boats\Droid\MainActivity.cs:37
[MonoDroid] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <IL 0x00013, 0x0008e>
[MonoDroid] at (wrapper dynamic-method) object.57e490e4-6d7f-42bb-8d74-c204b5bfcb8e (intptr,intptr,intptr) <IL 0x00017, 0x00027>
[art] JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable
必须重新安装软件包 "Microsoft.Bcl" 才能解决此问题..
这段完全相同的代码在另一个解决方案中可以正常工作,但在这个解决方案中却不行,所以我怀疑这是一个配置问题。
我的解决办法是: -XamForms PCL - Android 项目 - PCL 库(带有 INativeMethods) - Android 库(使用 NativeMethods 实现 INativeMethods)
这是我在 PCL 库中的界面:
namespace DreamzLibs
{
public interface INativeMethods
{
}
}
这是 Android 库中的实现:
[assembly: Dependency(typeof(NativeMethods))]
namespace DreamzLibs.Droid
{
public class NativeMethods : Java.Lang.Object, INativeMethods
{
}
}
这是请求的地方:
public static Lazy<INativeMethods> NativeMethods = new Lazy<INativeMethods>(() => DependencyService.Get<INativeMethods> ());
这是我得到的错误:
[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.TypeInitializationException: An exception was thrown by the type initializer for Boats.App ---> System.TypeLoadException: Could not load type 'DreamzLibs.Droid.NativeMethods' from assembly 'DreamzLibs.Droid, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
[MonoDroid] at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00023] in <filename unknown>:0
[MonoDroid] at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x000af] in <filename unknown>:0
[MonoDroid] at System.Activator.CreateInstance (System.Type type) [0x00000] in <filename unknown>:0
[MonoDroid] at Xamarin.Forms.DependencyService.Get[INativeMethods] (DependencyFetchTarget fetchTarget) [0x00076] in <filename unknown>:0
[MonoDroid] at DreamzLibs.Utils.<.cctor>b__2 () [0x00000] in e:\Code\Boats\DreamzLibs\Utils.cs:25
[MonoDroid] at System.Lazy`1[DreamzLibs.INativeMethods].CreateValue () [0x00075] in <filename unknown>:0
[MonoDroid] --- End of inner exception stack trace ---
[MonoDroid] at Boats.Droid.MainActivity.OnCreate (Android.OS.Bundle) [0x0005d] in e:\Code\Boats\Droid\MainActivity.cs:37
[MonoDroid] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <IL 0x00013, 0x0008e>
[MonoDroid] at (wrapper dynamic-method) object.57e490e4-6d7f-42bb-8d74-c204b5bfcb8e (intptr,intptr,intptr) <IL 0x00017, 0x00027>
[art] JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable
必须重新安装软件包 "Microsoft.Bcl" 才能解决此问题..