-3
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.” 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?

Temani Afif
  • 180,975
  • 14
  • 166
  • 216
Kaliwrki
  • 5
  • 1
  • 1
    @xanatos please don't edit to add your answer inside the question body. A question need to remain a *question*. If you think the duplicate isn't suitable vote to re-open and then answer the question – Temani Afif Jan 04 '21 at 13:55
  • It might be helpful if you add a comment behind the exact line that throws the exception, something like "... // System.NullReferenceException thrown here", since there are many similar lines in the code. – Greg Kramida Jan 04 '21 at 14:17

1 Answers1

0

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).

You inside Zoo have:

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

fields of type 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)

var propertyInfo = aggregatedObjType.GetProperties().FirstOrDefault(p => typeof(IList<TObj>).IsAssignableFrom(p.PropertyType));
xanatos
  • 102,557
  • 10
  • 176
  • 249