简体   繁体   English

System.NullReferenceException:“对象引用未设置为 object 的实例。” 问题

[英]System.NullReferenceException: „Object reference not set to an instance of an object.” problem

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();
            }
        }
    }
}

Basically, program gives expection "System.NullReferenceException: „Object reference not set to an instance of an object.”基本上,程序给出预期“System.NullReferenceException:“对象引用未设置为 object 的实例。” at Add function container.Set<C, IAction>().Add(element);. container.Set<C, IAction>() returns null and i dont know why. Funcion Add in main should Add new object to list "opiekunowie" in class zoo. What is the reason for that to not work? at Add function container.Set<C, IAction>().Add(element);. container.Set<C, IAction>() returns null and i dont know why. Funcion Add in main should Add new object to list "opiekunowie " 在 class 动物园里。那不起作用的原因是什么?

Your code uses reflection, but doesn't find what it wants, for two reasons.您的代码使用反射,但没有找到它想要的,有两个原因。

The reflection is done here:反射在这里完成:

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>;
}

It looks for properties of type IList<something> (this is reason 1 ).它查找IList<something>类型的属性(这是原因 1 )。

You inside Zoo have:你在Zoo里有:

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

fields of type List<something> List<something>类型的字段

So correct like this (to have properties):像这样正确(具有属性):

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

and then (to check for properties of a type assignable to IList<something> (this is reason 2 )然后(检查可分配给IList<something>的类型的属性(这是原因 2

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 会话{“对象引用未设置为对象的实例。”} System.Exception {System.NullReferenceException} - session {“Object reference not set to an instance of an object.”} System.Exception {System.NullReferenceException} System.NullReferenceException:对象引用未设置为对象的实例。 抛出错误 - System.NullReferenceException: Object reference not set to an instance of an object. throwing error System.NullReferenceException:未将对象引用设置为对象的实例。 在测试自动化执行期间 - System.NullReferenceException: Object reference not set to an instance of an object. During Test automation execution System.NullReferenceException未将对象引用设置为对象的实例。 关于模型实例化 - 有时候 - System.NullReferenceException Object reference not set to an instance of an object. on Model instantiation - SOMETIMES “ System.NullReferenceException-对象引用未设置为对象的实例。” Web方法,用户控件 - “System.NullReferenceException— Object reference not set to an instance of an object.” Webmethod, User control MVC:通过SelectListItem,我得到“ System.NullReferenceException:对象引用未设置为对象的实例。”错误 - MVC: By SelectListItem I get “System.NullReferenceException: Object reference not set to an instance of an object.” error 获取 System.NullReferenceException:“对象引用未设置为 object 的实例。” 调用服务时 - getting System.NullReferenceException: 'Object reference not set to an instance of an object.' when calling a service System.NullReferenceException-对象引用未设置为对象的实例。 C#中的原因 - System.NullReferenceException - Object reference not set to an instance of an object. Causes in C# System.NullReferenceException: &#39;未将对象引用设置为对象的实例。&#39; 这里的问题是什么? - System.NullReferenceException: 'Object reference not set to an instance of an object.' What is the Issue here? 编辑 System.NullReferenceException 时使用 DataTable 的 Datagrid 绑定:“未将对象引用设置为对象的实例。” - Datagrid binding using DataTable on edit System.NullReferenceException: 'Object reference not set to an instance of an object.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM