如果接口中有属性访问器,则Ninject会因NullRef异常而失败

时间:2012-02-09 13:22:05

标签: c# mono ninject

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

我有一个(超级)简单的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 

2 个答案:

答案 0 :(得分:6)

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

答案 1 :(得分:0)

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

@Ian Davis回答说,对于单声道,你应该使用单声道版本。 特别是对于统一,我认为它应该是mono 2.0版本。