如何提供 ProjectItem.get_FileNames(i) 的实现?

How do I provide an implementation of ProjectItem.get_FileNames(i)?

我正在尝试设置一个很好的 REPL 用于遍历 csharp 代码。但是我似乎无法编写 EnvDTE.ProjectItem (接口)的实现代码。

界面上行为不当的定义 属性 在索引器中为:

string FileNames[short i] {get;}

基于this post 我尝试了

[IndexerName("FileNames")]
string ProjectItem.this[short i] {get{return "test";}}

显式接口声明中的'this'不是接口

的成员
[IndexerName("FileNames")]
public string this[short i] {get{return "test";}}

returns 访问器 'UserQuery.ProjectItemFake.this[short].get' 无法为类型 'UserQuery.ProjectItemFake' 实现接口成员 'EnvDTE.ProjectItem.get_FileNames(short)'。使用显式接口实现。

[IndexerName("FileNames")]
string ProjectItem.this[short i] {get{return "test";}}

returns 'this' in explicit interface declaration is not a member of interface

我对 .net 语言完全开放,主要了解 C#、F# 或 VB.net。

你能以某种方式在 .net 中编写接口 EnvDTE.ProjectItem 的实现吗?

只需实现方法get_FileNames(short i)

属性(包括索引属性)实际上在幕后转换为 get_PropertyNameset_PropertyName 等方法。在大多数情况下,编译器不允许您实现这样的属性,只是因为它依赖于成员元数据。命名索引器在 C# 中不可用,但它们在 VB.NET 中可用,我认为该接口是在 VB.NET 中定义的(它们是 also available in F#,但我不确定它们是否兼容)。

C# 编译器允许您将 VB.NET 索引属性实现为 get_PropertyName(Whatever x),因为这是实现它们的唯一方法。我试过了,对我有用。

完整的C#实现如下:

class ProjectItemMock : ProjectItem{
    public bool SaveAs(string newFilename) { return false;}
    public EnvDTE.Window Open(string name) { return null;}
    public void Remove() {}
    public void ExpandView(){}
    public void Save(string filename){}
    public void Delete(){}
    public bool IsDirty {get;set;}
    public string get_FileNames(short index) {return "test";}
    public short FileCount {get;set;}
    public string Name{get;set;}
    public string Kind { get; set; }
    public EnvDTE.ProjectItems Collection {get;set;}
    public EnvDTE.Properties Properties {get;set;}
    public EnvDTE.DTE DTE{get;set;}
    public EnvDTE.ProjectItems ProjectItems { get; set; }
    public bool get_IsOpen(string s) { return false;}
    public object Object { get; set; }
    public object get_Extender(string s) {return null;}
    public object ExtenderNames { get; set; }
    public string ExtenderCATID { get; set; }
    public bool Saved { get; set; }
    public EnvDTE.ConfigurationManager ConfigurationManager { get; set; }
    public EnvDTE.FileCodeModel FileCodeModel { get; set; }
    public EnvDTE.Document Document { get; set; }
    public EnvDTE.Project SubProject { get; set; }
    public EnvDTE.Project ContainingProject { get; set; }
}

我怀疑 是正确的,因为类似的方法在 F#

中起作用

这是一个看起来工作正常的完整接口实现。

let makeProjectItem name kind fileNames fcm = 
    let name = ref name
    let isDirty = ref  false
    let saved = ref false
    let empty = null
    let fileNames:string[] = fileNames
    { 
        new EnvDTE.ProjectItem
        with
            member this.FileNames with get(index:int16) = fileNames.[ int(index)]
            member this.Open(viewKind) = null
            member this.Remove() = ()
            member this.get_IsOpen s = false
            member this.Kind = kind
            member this.ProjectItems = null
            member this.ExpandView() = ()
            member this.DTE = null
            member this.Properties = null
            member this.Object = null
            member this.Collection = null
            member this.Saved with get() = !saved and set(value) = saved := value
            member this.ExtenderCATID = null
            member this.Name 
                with get() = !name
                and set(value) = name := value
            member this.get_Extender (s:string) : obj = null
            member this.FileCount = 1s
            member this.SaveAs nfn = false
            member this.ContainingProject = null
            member this.SubProject = null
            member this.Document = null
            member this.Save fn = ()
            member this.IsDirty with get() = !isDirty  and set(value) = isDirty := value
            member this.Delete () = ()
            member this.get_FileCodeModel():EnvDTE.FileCodeModel = fcm
            member this.get_ExtenderNames() = null
            member this.get_ConfigurationManager () = null
    }