C# 错误CS1061:结构不包含';foo';没有可访问的扩展方法';foo';可以找到
我是C#的新手,试图学习统一。我尝试创建一个结构,然后扩展一个IList来包含它,但是结构的成员似乎没有被检测到。该列表用于RTS游戏中的订单对象 您可以通过一个新的unity项目来复制它,然后创建两个.cs文件HasFoo.cs和haswoolist.cs HasFoo.cs:C# 错误CS1061:结构不包含';foo';没有可访问的扩展方法';foo';可以找到,c#,unity3d,C#,Unity3d,我是C#的新手,试图学习统一。我尝试创建一个结构,然后扩展一个IList来包含它,但是结构的成员似乎没有被检测到。该列表用于RTS游戏中的订单对象 您可以通过一个新的unity项目来复制它,然后创建两个.cs文件HasFoo.cs和haswoolist.cs HasFoo.cs: using System.Collections; using System.Collections.Generic; using UnityEngine; public struct
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public struct HasFoo{
public int foo;
public HasFoo(bool constructor_parameter){
foo = 1;
}
}
hasgoullist.css:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HasFooList<HasFoo> : IList<HasFoo>{
private List<HasFoo> _list = new List<HasFoo>();
public void Add(HasFoo item){
item.foo = 2;
this._list.Add(item);
}
#region Implementation of IEnumerable
public IEnumerator<HasFoo> GetEnumerator()
{
return _list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#region Implementation of ICollection<HasFoo>
public void Clear()
{
_list.Clear();
}
public bool Contains(HasFoo item)
{
return _list.Contains(item);
}
public void CopyTo(HasFoo[] array, int arrayIndex)
{
_list.CopyTo(array, arrayIndex);
}
public bool Remove(HasFoo item)
{
return _list.Remove(item);
}
public int Count
{
get { return _list.Count; }
}
public bool IsReadOnly
{
get { return _list.IsReadOnly; }
}
#endregion
#region Implementation of IList<HasFoo>
public int IndexOf(HasFoo item)
{
return _list.IndexOf(item);
}
public void Insert(int index, HasFoo item)
{
_list.Insert(index, item);
}
public void RemoveAt(int index)
{
_list.RemoveAt(index);
}
public HasFoo this[int index]
{
get { return _list[index]; }
set { _list[index] = value; }
}
#endregion
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类愚人:IList{
私有列表_List=新列表();
公共作废添加(HasFoo项目){
item.foo=2;
此._列表。添加(项目);
}
#IEnumerable的区域实现
公共IEnumerator GetEnumerator()
{
返回_list.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
返回GetEnumerator();
}
#端区
#ICollection的区域实现
公共空间清除()
{
_list.Clear();
}
公共bool包含(HasFoo项)
{
退货清单包含(项目);
}
public void CopyTo(HasFoo[]数组,int-arrayIndex)
{
_CopyTo(数组,arrayIndex);
}
公共布尔删除(HasFoo项)
{
返回列表。删除(项目);
}
公共整数计数
{
获取{return\u list.Count;}
}
公共图书馆是只读的
{
获取{return\u list.IsReadOnly;}
}
#端区
#IList的区域实施
公共int IndexOf(HasFoo项目)
{
返回(项目)的列表索引;
}
公共作废插入(int索引,HasFoo项)
{
_列表。插入(索引,项目);
}
公共无效删除(整数索引)
{
_列表。删除(索引);
}
public HasFoo this[int index]
{
获取{return _list[index];}
设置{u列表[index]=value;}
}
#端区
}
我收到以下错误消息:
Assets/haswuist.cs(10,14):错误CS1061:“HasFoo”不包含“foo”的定义,并且找不到接受“HasFoo”类型的第一个参数的可访问扩展方法“foo”(是否缺少using指令或程序集引用?)
Assets/haswuist.cs(57,28):错误CS1061:“List”不包含“IsReadOnly”的定义,并且找不到接受“List”类型的第一个参数的可访问扩展方法“IsReadOnly”(是否缺少using指令或程序集引用?)
如果我将haswoolist的
IsReadOnly更改为简单地返回false,则该错误会消失,但显然并不理想。您的类不应具有类型参数,而应实现IList
:
公共类hasguist:IList{…}
如果我将haswoolist的IsReadOnly更改为只返回false
,则该错误会消失,但显然并不理想
为什么不呢haswoolist
显然不是只读的,因此在您的情况下,IsReadOnly
应该始终返回false
。列表的是只读的。如果要访问它,需要将\u list
强制转换为ICollection
或IList
,因为该属性是显式实现的,正如您在中所看到的。第一个错误的原因:haswoolist
,所以编译器认为HasFoo
是一个类型参数。将此更改为public class haswoolist:IList
考虑只使用列表,而不是创建自己的类,除非您有很好的理由需要额外的功能。如果你这样做,考虑只使用扩展方法到<代码>列表<代码>,而不是负担自己执行代码< IIST < /代码>的混乱。这里我要注意的是,你正在使用一个结构并对它进行变形。谢谢,它修复了它。我也不知道IsReadOnly也是这样的谢谢
public class HasFooList : IList<HasFoo> { ... }