如果接口中有属性访问器,Ninject 会失败并出现 NullRef 异常

发布于 2025-01-04 04:29:48 字数 8378 浏览 0 评论 0原文

我一直在寻找其他相关帖子,但没有找到任何内容,所以这里...

我有一个(超级)简单的 Ninject 测试,该测试因 NullReferenceExcption 失败,并且我找不到任何有关原因的信息。如果接口中定义了 C# 简化属性访问器,就会发生这种情况,否则没有问题。 如果我删除 get;放;功能按预期工作。

我的问题是我只是做了一些不应该工作的事情 - 例如这是否与已弃用的字段注入功能有关?

整个(失败的)示例非常短:

using System;
using Ninject;

namespace NinjectIFPropError
{
    public interface IDummy
    {
        int Number {get;set;}
        void Doit();
    }

    public class DummyClass : IDummy
    {
        public int Number {get;set;}

        public void Doit()
        {
            Console.WriteLine("Doit");
        }
    }

    class Program
    {
        public static void Main (string[] args)
        {
            IKernel kernel = new StandardKernel();
            kernel.Bind<IDummy>().To<DummyClass>();

            IDummy id = kernel.Get<IDummy>();
            id.Doit();  
        }
    }
}

我在 Ubuntu 11.10 上使用默认的 Mono 2.10.5 安装。 Ninject 是从 Ninject.org 下载的适用于 .Net 4.0 的 v2.2.1.0。

任何信息将不胜感激。

/A

我还附加了堆栈跟踪(相当长):

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
at Ninject.Infrastructure.Language.ExtensionsForMemberInfo.GetParentDefinition (System.Reflection.MethodInfo method, BindingFlags flags) [0x00000] in <filename unknown>:0 
at Ninject.Infrastructure.Language.ExtensionsForMemberInfo.GetParentDefinition (System.Reflection.PropertyInfo property) [0x00000] in <filename unknown>:0 
at Ninject.Infrastructure.Language.ExtensionsForMemberInfo.IsDefined (System.Reflection.PropertyInfo element, System.Type attributeType, Boolean inherit) [0x00000] in <filename unknown>:0 
at Ninject.Infrastructure.Language.ExtensionsForMemberInfo.HasAttribute (System.Reflection.MemberInfo member, System.Type type) [0x00000] in <filename unknown>:0 
at Ninject.Selection.Heuristics.StandardInjectionHeuristic.ShouldInject (System.Reflection.MemberInfo member) [0x00000] in <filename unknown>:0 
at Ninject.Selection.Selector+<>c__DisplayClass3.<SelectPropertiesForInjection>b__2 (IInjectionHeuristic h) [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable.Any[IInjectionHeuristic] (IEnumerable`1 source, System.Func`2 predicate) [0x00000] in <filename unknown>:0 
at Ninject.Selection.Selector.<SelectPropertiesForInjection>b__1 (System.Reflection.PropertyInfo p) [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable+<CreateWhereIterator>c__Iterator35`1[System.Reflection.PropertyInfo].MoveNext () [0x00000] in <filename unknown>:0 
at System.Collections.Generic.List`1[System.Reflection.PropertyInfo].AddEnumerable (IEnumerable`1 enumerable) [0x00000] in <filename unknown>:0 
at System.Collections.Generic.List`1[System.Reflection.PropertyInfo].AddRange (IEnumerable`1 collection) [0x00000] in <filename unknown>:0 
at Ninject.Selection.Selector.SelectPropertiesForInjection (System.Type type) [0x00000] in <filename unknown>:0 
at Ninject.Planning.Strategies.PropertyReflectionStrategy.Execute (IPlan plan) [0x00000] in <filename unknown>:0 
at Ninject.Planning.Planner+<>c__DisplayClass3.<GetPlan>b__1 (IPlanningStrategy s) [0x00000] in <filename unknown>:0 
at Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map[IPlanningStrategy] (IEnumerable`1 series, System.Action`1 action) [0x00000] in <filename unknown>:0 
at Ninject.Planning.Planner.GetPlan (System.Type type) [0x00000] in <filename unknown>:0 
at Ninject.Activation.Providers.StandardProvider.Create (IContext context) [0x00000] in <filename unknown>:0 
at Ninject.Activation.Context.Resolve () [0x00000] in <filename unknown>:0 
at Ninject.KernelBase.<Resolve>b__7 (IContext context) [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable+<CreateSelectIterator>c__Iterator27`2[Ninject.Activation.IContext,System.Object].MoveNext () [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable+<CreateCastIterator>c__Iterator17`1[NinjectIFPropError.IDummy].MoveNext () [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable.Single[IDummy] (IEnumerable`1 source, System.Func`2 predicate, Fallback fallback) [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable.Single[IDummy] (IEnumerable`1 source) [0x00000] in <filename unknown>:0 
at Ninject.ResolutionExtensions.Get[IDummy] (IResolutionRoot root, Ninject.Parameters.IParameter[] parameters) [0x00000] in <filename unknown>:0 
at NinjectIFPropError.Program.Main (System.String[] args) [0x00018] in /home/roger/Projects/NinjectIFPropError/Main.cs:38 
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
at Ninject.Infrastructure.Language.ExtensionsForMemberInfo.GetParentDefinition (System.Reflection.MethodInfo method, BindingFlags flags) [0x00000] in <filename unknown>:0 
at Ninject.Infrastructure.Language.ExtensionsForMemberInfo.GetParentDefinition (System.Reflection.PropertyInfo property) [0x00000] in <filename unknown>:0 
at Ninject.Infrastructure.Language.ExtensionsForMemberInfo.IsDefined (System.Reflection.PropertyInfo element, System.Type attributeType, Boolean inherit) [0x00000] in <filename unknown>:0 
at Ninject.Infrastructure.Language.ExtensionsForMemberInfo.HasAttribute (System.Reflection.MemberInfo member, System.Type type) [0x00000] in <filename unknown>:0 
at Ninject.Selection.Heuristics.StandardInjectionHeuristic.ShouldInject (System.Reflection.MemberInfo member) [0x00000] in <filename unknown>:0 
at Ninject.Selection.Selector+<>c__DisplayClass3.<SelectPropertiesForInjection>b__2 (IInjectionHeuristic h) [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable.Any[IInjectionHeuristic] (IEnumerable`1 source, System.Func`2 predicate) [0x00000] in <filename unknown>:0 
at Ninject.Selection.Selector.<SelectPropertiesForInjection>b__1 (System.Reflection.PropertyInfo p) [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable+<CreateWhereIterator>c__Iterator35`1[System.Reflection.PropertyInfo].MoveNext () [0x00000] in <filename unknown>:0 
at System.Collections.Generic.List`1[System.Reflection.PropertyInfo].AddEnumerable (IEnumerable`1 enumerable) [0x00000] in <filename unknown>:0 
at System.Collections.Generic.List`1[System.Reflection.PropertyInfo].AddRange (IEnumerable`1 collection) [0x00000] in <filename unknown>:0 
at Ninject.Selection.Selector.SelectPropertiesForInjection (System.Type type) [0x00000] in <filename unknown>:0 
at Ninject.Planning.Strategies.PropertyReflectionStrategy.Execute (IPlan plan) [0x00000] in <filename unknown>:0 
at Ninject.Planning.Planner+<>c__DisplayClass3.<GetPlan>b__1 (IPlanningStrategy s) [0x00000] in <filename unknown>:0 
at Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map[IPlanningStrategy] (IEnumerable`1 series, System.Action`1 action) [0x00000] in <filename unknown>:0 
at Ninject.Planning.Planner.GetPlan (System.Type type) [0x00000] in <filename unknown>:0 
at Ninject.Activation.Providers.StandardProvider.Create (IContext context) [0x00000] in <filename unknown>:0 
at Ninject.Activation.Context.Resolve () [0x00000] in <filename unknown>:0 
at Ninject.KernelBase.<Resolve>b__7 (IContext context) [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable+<CreateSelectIterator>c__Iterator27`2[Ninject.Activation.IContext,System.Object].MoveNext () [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable+<CreateCastIterator>c__Iterator17`1[NinjectIFPropError.IDummy].MoveNext () [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable.Single[IDummy] (IEnumerable`1 source, System.Func`2 predicate, Fallback fallback) [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable.Single[IDummy] (IEnumerable`1 source) [0x00000] in <filename unknown>:0 
at Ninject.ResolutionExtensions.Get[IDummy] (IResolutionRoot root, Ninject.Parameters.IParameter[] parameters) [0x00000] in <filename unknown>:0 
at NinjectIFPropError.Program.Main (System.String[] args) [0x00018] in /home/roger/Projects/NinjectIFPropError/Main.cs:38 

I've been searching for other relevant posts but I didn't find anything, so here goes...

I have a (super)simple Ninject test that fails with a NullReferenceExcption and I can't find any info on why. It happens if there is a C# simplified property accessor defined in the interface, otherwise it's ok.
If I remove the get; set; functionality it works as expected.

My question is am I simply doing something that shouldn't work - e.g. is this related to th deprecated field injection functionality?

The whole (failing) example is very short:

using System;
using Ninject;

namespace NinjectIFPropError
{
    public interface IDummy
    {
        int Number {get;set;}
        void Doit();
    }

    public class DummyClass : IDummy
    {
        public int Number {get;set;}

        public void Doit()
        {
            Console.WriteLine("Doit");
        }
    }

    class Program
    {
        public static void Main (string[] args)
        {
            IKernel kernel = new StandardKernel();
            kernel.Bind<IDummy>().To<DummyClass>();

            IDummy id = kernel.Get<IDummy>();
            id.Doit();  
        }
    }
}

I'm using the default Mono 2.10.5 install on Ubuntu 11.10. Ninject is v2.2.1.0 for .Net 4.0 downloaded from Ninject.org.

Any info would be appreciated.

/A

I'm appending the stack trace as well (pretty long):

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
at Ninject.Infrastructure.Language.ExtensionsForMemberInfo.GetParentDefinition (System.Reflection.MethodInfo method, BindingFlags flags) [0x00000] in <filename unknown>:0 
at Ninject.Infrastructure.Language.ExtensionsForMemberInfo.GetParentDefinition (System.Reflection.PropertyInfo property) [0x00000] in <filename unknown>:0 
at Ninject.Infrastructure.Language.ExtensionsForMemberInfo.IsDefined (System.Reflection.PropertyInfo element, System.Type attributeType, Boolean inherit) [0x00000] in <filename unknown>:0 
at Ninject.Infrastructure.Language.ExtensionsForMemberInfo.HasAttribute (System.Reflection.MemberInfo member, System.Type type) [0x00000] in <filename unknown>:0 
at Ninject.Selection.Heuristics.StandardInjectionHeuristic.ShouldInject (System.Reflection.MemberInfo member) [0x00000] in <filename unknown>:0 
at Ninject.Selection.Selector+<>c__DisplayClass3.<SelectPropertiesForInjection>b__2 (IInjectionHeuristic h) [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable.Any[IInjectionHeuristic] (IEnumerable`1 source, System.Func`2 predicate) [0x00000] in <filename unknown>:0 
at Ninject.Selection.Selector.<SelectPropertiesForInjection>b__1 (System.Reflection.PropertyInfo p) [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable+<CreateWhereIterator>c__Iterator35`1[System.Reflection.PropertyInfo].MoveNext () [0x00000] in <filename unknown>:0 
at System.Collections.Generic.List`1[System.Reflection.PropertyInfo].AddEnumerable (IEnumerable`1 enumerable) [0x00000] in <filename unknown>:0 
at System.Collections.Generic.List`1[System.Reflection.PropertyInfo].AddRange (IEnumerable`1 collection) [0x00000] in <filename unknown>:0 
at Ninject.Selection.Selector.SelectPropertiesForInjection (System.Type type) [0x00000] in <filename unknown>:0 
at Ninject.Planning.Strategies.PropertyReflectionStrategy.Execute (IPlan plan) [0x00000] in <filename unknown>:0 
at Ninject.Planning.Planner+<>c__DisplayClass3.<GetPlan>b__1 (IPlanningStrategy s) [0x00000] in <filename unknown>:0 
at Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map[IPlanningStrategy] (IEnumerable`1 series, System.Action`1 action) [0x00000] in <filename unknown>:0 
at Ninject.Planning.Planner.GetPlan (System.Type type) [0x00000] in <filename unknown>:0 
at Ninject.Activation.Providers.StandardProvider.Create (IContext context) [0x00000] in <filename unknown>:0 
at Ninject.Activation.Context.Resolve () [0x00000] in <filename unknown>:0 
at Ninject.KernelBase.<Resolve>b__7 (IContext context) [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable+<CreateSelectIterator>c__Iterator27`2[Ninject.Activation.IContext,System.Object].MoveNext () [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable+<CreateCastIterator>c__Iterator17`1[NinjectIFPropError.IDummy].MoveNext () [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable.Single[IDummy] (IEnumerable`1 source, System.Func`2 predicate, Fallback fallback) [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable.Single[IDummy] (IEnumerable`1 source) [0x00000] in <filename unknown>:0 
at Ninject.ResolutionExtensions.Get[IDummy] (IResolutionRoot root, Ninject.Parameters.IParameter[] parameters) [0x00000] in <filename unknown>:0 
at NinjectIFPropError.Program.Main (System.String[] args) [0x00018] in /home/roger/Projects/NinjectIFPropError/Main.cs:38 
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
at Ninject.Infrastructure.Language.ExtensionsForMemberInfo.GetParentDefinition (System.Reflection.MethodInfo method, BindingFlags flags) [0x00000] in <filename unknown>:0 
at Ninject.Infrastructure.Language.ExtensionsForMemberInfo.GetParentDefinition (System.Reflection.PropertyInfo property) [0x00000] in <filename unknown>:0 
at Ninject.Infrastructure.Language.ExtensionsForMemberInfo.IsDefined (System.Reflection.PropertyInfo element, System.Type attributeType, Boolean inherit) [0x00000] in <filename unknown>:0 
at Ninject.Infrastructure.Language.ExtensionsForMemberInfo.HasAttribute (System.Reflection.MemberInfo member, System.Type type) [0x00000] in <filename unknown>:0 
at Ninject.Selection.Heuristics.StandardInjectionHeuristic.ShouldInject (System.Reflection.MemberInfo member) [0x00000] in <filename unknown>:0 
at Ninject.Selection.Selector+<>c__DisplayClass3.<SelectPropertiesForInjection>b__2 (IInjectionHeuristic h) [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable.Any[IInjectionHeuristic] (IEnumerable`1 source, System.Func`2 predicate) [0x00000] in <filename unknown>:0 
at Ninject.Selection.Selector.<SelectPropertiesForInjection>b__1 (System.Reflection.PropertyInfo p) [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable+<CreateWhereIterator>c__Iterator35`1[System.Reflection.PropertyInfo].MoveNext () [0x00000] in <filename unknown>:0 
at System.Collections.Generic.List`1[System.Reflection.PropertyInfo].AddEnumerable (IEnumerable`1 enumerable) [0x00000] in <filename unknown>:0 
at System.Collections.Generic.List`1[System.Reflection.PropertyInfo].AddRange (IEnumerable`1 collection) [0x00000] in <filename unknown>:0 
at Ninject.Selection.Selector.SelectPropertiesForInjection (System.Type type) [0x00000] in <filename unknown>:0 
at Ninject.Planning.Strategies.PropertyReflectionStrategy.Execute (IPlan plan) [0x00000] in <filename unknown>:0 
at Ninject.Planning.Planner+<>c__DisplayClass3.<GetPlan>b__1 (IPlanningStrategy s) [0x00000] in <filename unknown>:0 
at Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map[IPlanningStrategy] (IEnumerable`1 series, System.Action`1 action) [0x00000] in <filename unknown>:0 
at Ninject.Planning.Planner.GetPlan (System.Type type) [0x00000] in <filename unknown>:0 
at Ninject.Activation.Providers.StandardProvider.Create (IContext context) [0x00000] in <filename unknown>:0 
at Ninject.Activation.Context.Resolve () [0x00000] in <filename unknown>:0 
at Ninject.KernelBase.<Resolve>b__7 (IContext context) [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable+<CreateSelectIterator>c__Iterator27`2[Ninject.Activation.IContext,System.Object].MoveNext () [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable+<CreateCastIterator>c__Iterator17`1[NinjectIFPropError.IDummy].MoveNext () [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable.Single[IDummy] (IEnumerable`1 source, System.Func`2 predicate, Fallback fallback) [0x00000] in <filename unknown>:0 
at System.Linq.Enumerable.Single[IDummy] (IEnumerable`1 source) [0x00000] in <filename unknown>:0 
at Ninject.ResolutionExtensions.Get[IDummy] (IResolutionRoot root, Ninject.Parameters.IParameter[] parameters) [0x00000] in <filename unknown>:0 
at NinjectIFPropError.Program.Main (System.String[] args) [0x00018] in /home/roger/Projects/NinjectIFPropError/Main.cs:38 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放血 2025-01-11 04:29:48

在 Mono 环境中运行 Ninject 时,您需要确保使用 Mono 构建。如果您转到 TeamCity 构建服务器,您可以获得工件适用于 Mono 2.0、3.5 和 4.0 版本。请尝试看看您的问题是否得到解决。

You need to make sure you are using the Mono build when running Ninject in a Mono environment. If you go to the TeamCity build server you can get the artifacts for the Mono 2.0, 3.5, and 4.0 builds. Please try them to see if your issue is resolved.

夏日落 2025-01-11 04:29:48

对于那些来自 Unity3D 应用程序的人:

正如@Ian Davis 回答的那样,对于单声道,您应该使用单声道构建。
特别是对于统一性,我认为应该是 mono 2.0 版本。

For those coming here from a Unity3D application:

As @Ian Davis answered, for mono you should use the mono builds.
For unity in particular I think it should be the mono 2.0 build.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文