Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.NullReferenceException:&x201E;对象引用未设置为对象的实例;问题 使用系统; 名称空间zestaw_6 { 公共课程 { 公共静态void Main(字符串[]args) { var zoo=new zoo(); 添加(新的Opiekun(“Jan”、“Kowalski”); Console.ReadKey(); } } 使用系统; 使用System.Collections.Generic; 使用系统文本; 使用System.Linq; 名称空间zestaw_6 { 公共静态类ActionExtensions { 公共静态IList集(此标记聚合对象),其中TObj:IInfo,其中TAg:IAction { var aggregatedObjType=aggregatedObj.GetType(); var propertyInfo=aggregatedObjType.GetProperties().FirstOrDefault(p=>p.PropertyType==typeof(IList)); var propertyValue=propertyInfo?.GetValue(aggregatedObj); 将propertyValue返回为IList; } 公共静态C Get(此IAAction容器,Func searchPredicate=null),其中C:IInfo { return searchPredicate==null?container.Set().FirstOrDefault():container.Set().FirstOrDefault(searchPredicate); } 公共静态IList GetList(此IAction容器,Func searchPredicate=null),其中C:IInfo { return searchPredicate==null?container.Set():container.Set().Where(searchPredicate.ToList(); } 公共静态S Add(这个S容器,C元素),其中S:IAction,其中C:IInfo { container.Set().Add(元素); 返回容器; } 公共静态C Remove(此IAction容器,Func searchFn),其中C:IInfo { var obj=container.Set().SingleOrDefault(searchFn); 如果(obj!=null) { container.Set().Remove(obj); } 返回obj; } 公共静态C加载项(此C对象,IAction容器),其中C:IInfo { container.Set().Add(obj); 返回obj; } 公共静态void ForEach(此IList列表,Action-Action),其中T:IInfo { for(int i=0;itypeof(IList).IsAssignableFrom(p.PropertyType)); @xanatos请不要编辑以将答案添加到问题正文中。问题需要保留为问题。如果您认为副本不适合重新打开并回答问题,则在引发异常的确切行后面添加注释可能会有所帮助,例如“..//System.NullReferenceException在此引发”,因为代码中有许多类似的行。 public List<Klatka> klatki = new List<Klatka>(); public List<Opiekun> opiekunowie = new List<Opiekun>(); public List<Klatka> klatki { get; set; } = new List<Klatka>(); public List<Opiekun> opiekunowie { get; set; } = new List<Opiekun>(); var propertyInfo = aggregatedObjType.GetProperties().FirstOrDefault(p => typeof(IList<TObj>).IsAssignableFrom(p.PropertyType));_C#_Nullreferenceexception - Fatal编程技术网

C# System.NullReferenceException:&x201E;对象引用未设置为对象的实例;问题 使用系统; 名称空间zestaw_6 { 公共课程 { 公共静态void Main(字符串[]args) { var zoo=new zoo(); 添加(新的Opiekun(“Jan”、“Kowalski”); Console.ReadKey(); } } 使用系统; 使用System.Collections.Generic; 使用系统文本; 使用System.Linq; 名称空间zestaw_6 { 公共静态类ActionExtensions { 公共静态IList集(此标记聚合对象),其中TObj:IInfo,其中TAg:IAction { var aggregatedObjType=aggregatedObj.GetType(); var propertyInfo=aggregatedObjType.GetProperties().FirstOrDefault(p=>p.PropertyType==typeof(IList)); var propertyValue=propertyInfo?.GetValue(aggregatedObj); 将propertyValue返回为IList; } 公共静态C Get(此IAAction容器,Func searchPredicate=null),其中C:IInfo { return searchPredicate==null?container.Set().FirstOrDefault():container.Set().FirstOrDefault(searchPredicate); } 公共静态IList GetList(此IAction容器,Func searchPredicate=null),其中C:IInfo { return searchPredicate==null?container.Set():container.Set().Where(searchPredicate.ToList(); } 公共静态S Add(这个S容器,C元素),其中S:IAction,其中C:IInfo { container.Set().Add(元素); 返回容器; } 公共静态C Remove(此IAction容器,Func searchFn),其中C:IInfo { var obj=container.Set().SingleOrDefault(searchFn); 如果(obj!=null) { container.Set().Remove(obj); } 返回obj; } 公共静态C加载项(此C对象,IAction容器),其中C:IInfo { container.Set().Add(obj); 返回obj; } 公共静态void ForEach(此IList列表,Action-Action),其中T:IInfo { for(int i=0;itypeof(IList).IsAssignableFrom(p.PropertyType)); @xanatos请不要编辑以将答案添加到问题正文中。问题需要保留为问题。如果您认为副本不适合重新打开并回答问题,则在引发异常的确切行后面添加注释可能会有所帮助,例如“..//System.NullReferenceException在此引发”,因为代码中有许多类似的行。 public List<Klatka> klatki = new List<Klatka>(); public List<Opiekun> opiekunowie = new List<Opiekun>(); public List<Klatka> klatki { get; set; } = new List<Klatka>(); public List<Opiekun> opiekunowie { get; set; } = new List<Opiekun>(); var propertyInfo = aggregatedObjType.GetProperties().FirstOrDefault(p => typeof(IList<TObj>).IsAssignableFrom(p.PropertyType));

C# System.NullReferenceException:&x201E;对象引用未设置为对象的实例;问题 使用系统; 名称空间zestaw_6 { 公共课程 { 公共静态void Main(字符串[]args) { var zoo=new zoo(); 添加(新的Opiekun(“Jan”、“Kowalski”); Console.ReadKey(); } } 使用系统; 使用System.Collections.Generic; 使用系统文本; 使用System.Linq; 名称空间zestaw_6 { 公共静态类ActionExtensions { 公共静态IList集(此标记聚合对象),其中TObj:IInfo,其中TAg:IAction { var aggregatedObjType=aggregatedObj.GetType(); var propertyInfo=aggregatedObjType.GetProperties().FirstOrDefault(p=>p.PropertyType==typeof(IList)); var propertyValue=propertyInfo?.GetValue(aggregatedObj); 将propertyValue返回为IList; } 公共静态C Get(此IAAction容器,Func searchPredicate=null),其中C:IInfo { return searchPredicate==null?container.Set().FirstOrDefault():container.Set().FirstOrDefault(searchPredicate); } 公共静态IList GetList(此IAction容器,Func searchPredicate=null),其中C:IInfo { return searchPredicate==null?container.Set():container.Set().Where(searchPredicate.ToList(); } 公共静态S Add(这个S容器,C元素),其中S:IAction,其中C:IInfo { container.Set().Add(元素); 返回容器; } 公共静态C Remove(此IAction容器,Func searchFn),其中C:IInfo { var obj=container.Set().SingleOrDefault(searchFn); 如果(obj!=null) { container.Set().Remove(obj); } 返回obj; } 公共静态C加载项(此C对象,IAction容器),其中C:IInfo { container.Set().Add(obj); 返回obj; } 公共静态void ForEach(此IList列表,Action-Action),其中T:IInfo { for(int i=0;itypeof(IList).IsAssignableFrom(p.PropertyType)); @xanatos请不要编辑以将答案添加到问题正文中。问题需要保留为问题。如果您认为副本不适合重新打开并回答问题,则在引发异常的确切行后面添加注释可能会有所帮助,例如“..//System.NullReferenceException在此引发”,因为代码中有许多类似的行。 public List<Klatka> klatki = new List<Klatka>(); public List<Opiekun> opiekunowie = new List<Opiekun>(); public List<Klatka> klatki { get; set; } = new List<Klatka>(); public List<Opiekun> opiekunowie { get; set; } = new List<Opiekun>(); var propertyInfo = aggregatedObjType.GetProperties().FirstOrDefault(p => typeof(IList<TObj>).IsAssignableFrom(p.PropertyType));,c#,nullreferenceexception,C#,Nullreferenceexception,基本上,程序在添加函数container.set().Add(element);.container.set()返回null,我不知道为什么。函数外接程序main应将新对象添加到列表“opiekunowie”中“在类动物园中。这不起作用的原因是什么?您的代码使用反射,但找不到它想要的东西,原因有二 反射在这里完成: public static IList<TObj> Set<TObj, TAg>(this TAg aggregatedObj) where TObj : I

基本上,程序在添加函数container.set().Add(element);.container.set()返回null,我不知道为什么。函数外接程序main应将新对象添加到列表“opiekunowie”中“在类动物园中。这不起作用的原因是什么?

您的代码使用反射,但找不到它想要的东西,原因有二

反射在这里完成:

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 List klatki{get;set;}=new List();
公共列表opiekunowie{get;set;}=new List();
然后(检查可分配给
IList
的类型的属性)(这是原因2)

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

@xanatos请不要编辑以将答案添加到问题正文中。问题需要保留为问题。如果您认为副本不适合重新打开并回答问题,则在引发异常的确切行后面添加注释可能会有所帮助,例如“..//System.NullReferenceException在此引发”,因为代码中有许多类似的行。
public List<Klatka> klatki = new List<Klatka>();
public List<Opiekun> opiekunowie = new List<Opiekun>();
public List<Klatka> klatki { get; set; } = new List<Klatka>();
public List<Opiekun> opiekunowie { get; set; } = new List<Opiekun>();
var propertyInfo = aggregatedObjType.GetProperties().FirstOrDefault(p => typeof(IList<TObj>).IsAssignableFrom(p.PropertyType));