【发布时间】:2018-07-07 04:09:41
【问题描述】:
这个问题与this question、this question、this question(相同的错误,不同的源类)和this question(相同的错误,不同的原因)非常相似。
在项目中编译我的程序以检查错误后,我收到以下 CS1061 错误:
Entity.cs(291,24): 错误 CS1061: '
List<Entity>' 不包含 'Item' 的定义并且没有扩展方法 'Item' 接受第一个 可以找到类型为“List<Entity>”的参数(您是否缺少 使用指令还是程序集引用?)
Entity.cs是发生错误的文件名,该错误发生在Load()函数中,如下图:
public void Load(){
/*
Try to spawn the entity.
If there are too many entities
on-screen, unload any special
effect entities.
If that fails, produce an error message
*/
try{
if(EntitiesArray.Count >= EntitiesOnScreenCap){
if(this.Type == EntityType.SpecialEffect)
Unload();
else
throw null;
}else
//Place the entity in the first available slot in the array
for(int i = 0; i < EntitiesArray.Capacity; i++)
if(EntitiesArray.Item[i] == NullEntity)
EntitiesArray.Item[i] = this;
}catch(Exception exc){
throw new LoadException("Failed to load entity.");
}
}
错误发生在这些行(第 291 和 292 行):
if(EntitiesArray.Item[i] == NullEntity)
EntitiesArray.Item[i] = this;
NullEntity 是 Entity 类型的字段,this 也是 Entity 类型的字段。
EntitesArray 是一个 List<Entity>,因此,根据 MSDN 文档 here,应该有一个 Item[] 属性。Entity 没有名为 Item 的方法或数组。
开课声明Entity:
public static List<Entity> EntitiesArray;
在Entity 中保证只运行一次的方法中的实例化:
EntitiesArray = new List<Entity>(EntitiesOnScreenCap);
字段 EntitiesOnScreenCap 是一个等于 200 的 int。
这包含在最高范围内(在命名空间之前),因此对此应该没有任何问题:
using System.Collections.Generic;
是什么导致了这个 CS1061 错误,我该如何解决?
【问题讨论】:
标签: c# compiler-errors generic-list