项目"具有索引和财产命名为&QUOT类; [英] Class with indexer and property named "Item"
本文介绍了项目"具有索引和财产命名为&QUOT类;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在.NET 4创建一个类:
Is it possible to create a class in .NET 4 with:
- 在一个索引,
- 在一个名为项财产?
例如,这个C#类将被编译为我:
For example, this C# class will not compile for me:
public class MyClass
{
public object Item { get; set; }
public object this[string index] { get { return null; } set { } }
}
编译器给出错误 CS0102 一>:
该类型'MyClass的'已经包含了'项目'
The type 'MyClass' already contains a definition for 'Item'
虽然我只是明确定义项目
键。
although I am only explicitly defining Item
once.
推荐答案
根据的这个网站,就可以使用属性来重命名索引
Based on this site, it is possible to use an attribute to rename the Indexer
public class MyClass
{
public object Item { get; set; }
[System.Runtime.CompilerServices.IndexerName("MyItem")]
public object this[string index] { get { return null; } set { } }
}
这篇关于项目"具有索引和财产命名为&QUOT类;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文