C#索引器必须至少有一个参数

本文关键字:有一个 参数 索引 | 更新日期: 2023-09-27 18:14:17

我继承了一些代码,当我尝试运行代码时,会收到上面的错误消息。以下是代码:

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Scripting
{
      [CompilerGenerated]
      [Guid("C7C3F5A0-88A3-11D0-ABCB-00A0C90FFFC0")]    
      [TypeIdentifier]    
      [ComImport]    
      public interface IDrive    
      {   
           [DispId(0)]    
           [IndexerName("Path")]    
           string this[] { [DispId(0)] get; } //The error is here//    
           [DispId(10009)]
           int SerialNumber { [DispId(10009)] get; }
           [DispId(10007)]
           string VolumeName { [DispId(10007)] get; [DispId(10007)] set; }
           [SpecialName]
           [MethodImpl(MethodCodeType = MethodCodeType.Runtime)]
           void _VtblGap1_7();
           [SpecialName]
           [MethodImpl(MethodCodeType = MethodCodeType.Runtime)]
           void _VtblGap2_1();
      }
}

我是C#的新手,想知道缺少什么参数。

我无法询问原始编码器。如有任何帮助,我们将不胜感激。

C#索引器必须至少有一个参数

就像错误所说的那样,"索引器必须至少有一个参数"。

因此,您需要向索引器添加一个参数,例如

string this[int index] { [DispId(0)] get; }

如果你仔细想想,当你使用索引器时,来提供一个整数作为参数。

例如

string path = myIDrive[0]; // Use the integer parameter to access the element
var wut = myIDrive[?]; // without any parameter, how would you get the Path data?
string this[] { [DispId(0)] get; }

正如错误所示,您缺少参数。

string this[object myIndexerParameter] 
{ 
    get 
    { 
         // return some value based on the parameter passed.  
    }
}

然后你这样称呼它:var something = myIDriveInstance[myIndexValue];

http://msdn.microsoft.com/en-us/library/6x16t2tx.aspx

这与List<T>允许传入项目的索引基本相同;因此命名为CCD_ 3。