Mik*_*ley 1 .net c# compiler-errors indexer
可能重复:
具有索引器的类和名为"Item"的属性
刚刚遇到过我以前从未见过的东西,并想知道为什么会发生这种情况?
使用下面的类,我得到编译器错误"已经声明了具有相同名称的成员",关于"Item"和"this [...]".
public class SomeClass : IDataErrorInfo
{
public int Item { get; set; }
public string this[string propertyName]
{
get
{
if (propertyName == "Item" && Item <= 0)
{
return "Item must be greater than 0";
}
return null;
}
}
public string Error
{
get { return null; }
}
}
Run Code Online (Sandbox Code Playgroud)
编译器似乎认为这个[...]和Item使用相同的成员名称.这是正确/正常吗?我很惊讶我之前没遇到过这个.
当您像这样定义索引器时:
this[string propertyName]
Run Code Online (Sandbox Code Playgroud)
它被编译到.Item属性中.
您可以使用[System.Runtime.CompilerServices.IndexerName("NEW NAME FOR YOUR PROPERTY")]属性将其修复为索引器.
| 归档时间: |
|
| 查看次数: |
1847 次 |
| 最近记录: |