【问题标题】:System.NullReferenceException: „Object reference not set to an instance of an object.” problemSystem.NullReferenceException:“对象引用未设置为对象的实例。”问题
【发布时间】:2021-04-09 15:31:51
【问题描述】:
using System;

namespace zestaw_6
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var zoo = new Zoo();
            zoo.Add(new Opiekun("Jan", "Kowalski"));
            Console.ReadKey();
        }
    }
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace zestaw_6
{
    public static class ActionExtensions
    {
        public static IList<TObj> Set<TObj, TAg>(this TAg aggregatedObj) where TObj : IInfo where TAg : IAction
        {
            var aggregatedObjType = aggregatedObj.GetType();
            var propertyInfo = aggregatedObjType.GetProperties().FirstOrDefault(p => p.PropertyType == typeof(IList<TObj>));
            var propertyValue = propertyInfo?.GetValue(aggregatedObj);
            return propertyValue as IList<TObj>;
        }
        public static C Get<C>(this IAction container, Func<C, bool> searchPredicate = null) where C : IInfo
        {
            return searchPredicate == null ? container.Set<C, IAction>().FirstOrDefault() : container.Set<C, IAction>().FirstOrDefault(searchPredicate);
        }
        public static IList<C> GetList<C>(this IAction container, Func<C, bool> searchPredicate = null) where C : IInfo
        {
            return searchPredicate == null ? container.Set<C, IAction>() : container.Set<C, IAction>().Where(searchPredicate).ToList();
        }
        public static S Add<C, S>(this S container, C element) where S : IAction where C : IInfo
        {
            container.Set<C, IAction>().Add(element);
            return container;
        }
        public static C Remove<C>(this IAction container, Func<C, bool> searchFn) where C : IInfo
        {
            var obj = container.Set<C, IAction>().SingleOrDefault(searchFn);
            if(obj != null)
            {
                container.Set<C, IAction>().Remove(obj);
            }
            return obj;
        }
        public static C AddInto<C>(this C obj, IAction container) where C : IInfo
        {
            container.Set<C, IAction>().Add(obj);
            return obj;
        }
        public static void ForEach<T>(this IList<T> list, Action<T> action) where T : IInfo
        {
            for(int i = 0; i < list.Count; i++)
            {
                action(list[i]);
            }
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;
namespace zestaw_6
{
    public class Zoo : IInfo, IAction
    {
        public List<Klatka> klatki = new List<Klatka>();
        public List<Opiekun> opiekunowie = new List<Opiekun>();
        public void DisplayInfo()
        {
            foreach (var item in klatki)
            {
                item.DisplayInfo();
            }
            foreach (var item in opiekunowie)
            {
                item.DisplayInfo();
            }
        }
    } 
}
using System;
using System.Collections.Generic;
using System.Text;

namespace zestaw_6
{
    public class Opiekun : IInfo, IAction
    {
        public List<Klatka> klatki = new List<Klatka>();
        public string imie { get; set; }
        public string nazwisko { get; set; }
        public Opiekun(string imie_, string nazwisko_)
        {
            imie = imie_;
            nazwisko = nazwisko_;
        }
        public void DisplayInfo()
        {
            Console.WriteLine("Imie: {0}", imie);
            Console.WriteLine("Nazwisko: {0}", nazwisko);
            foreach (var item in klatki)
            {
                item.DisplayInfo();
            }
        }
    }
}

基本上,程序给出期望“System.NullReferenceException:“对象引用未设置为对象的实例。”在添加函数 container.Set().Add(element);.container.Set() 返回 null,我不知道为什么。在 main 中添加函数应该将新对象添加到列表“opiekunowie ” 在课堂动物园。这是什么原因不起作用?

【问题讨论】:

  • @xanatos 请不要编辑以在问题正文中添加您的答案。一个问题需要保持一个问题。如果您认为副本不适合重新打开然后回答问题
  • 如果您在引发异常的确切行后面添加注释可能会有所帮助,例如“... // System.NullReferenceException throw here”,因为代码中有许多类似的行.

标签: c# nullreferenceexception


【解决方案1】:

您的代码使用反射,但没有找到它想要的,原因有两个。

反射在这里完成:

public static IList<TObj> Set<TObj, TAg>(this TAg aggregatedObj) where TObj : IInfo where TAg : IAction
{
    var aggregatedObjType = aggregatedObj.GetType();
    var propertyInfo = aggregatedObjType.GetProperties().FirstOrDefault(p => p.PropertyType == typeof(IList<TObj>));
    var propertyValue = propertyInfo?.GetValue(aggregatedObj);
    return propertyValue as IList<TObj>;
}

它查找 IList&lt;something&gt; 类型的 属性(这是原因 1)。

Zoo 里面的你有:

public List<Klatka> klatki = new List<Klatka>();
public List<Opiekun> opiekunowie = new List<Opiekun>();

字段,类型为 List&lt;something&gt;

这样正确(具有属性):

public List<Klatka> klatki { get; set; } = new List<Klatka>();
public List<Opiekun> opiekunowie { get; set; } = new List<Opiekun>();

然后(检查 可分配给 IList&lt;something&gt; 类型的属性(这是原因 2

var propertyInfo = aggregatedObjType.GetProperties().FirstOrDefault(p => typeof(IList<TObj>).IsAssignableFrom(p.PropertyType));

【讨论】:

    猜你喜欢
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-05
    • 2021-09-24
    相关资源
    最近更新 更多