gpt4 book ai didi

c# - 如果接口(interface)中有属性访问器,Ninject 将失败并出现 NullRef 异常

转载 作者:太空宇宙 更新时间:2023-11-03 19:25:14 25 4
gpt4 key购买 nike

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

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

我的问题是我只是在做一些不应该起作用的事情 - 例如这与已弃用的字段注入(inject)功能有关吗?

整个(失败的)例子很短:

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。

如有任何信息,我们将不胜感激。

/一个

我也附加了堆栈跟踪(很长):

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

最佳答案

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

关于c# - 如果接口(interface)中有属性访问器,Ninject 将失败并出现 NullRef 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9211627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com