Search code examples
c#.netenvdteindexer

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


I'm trying to set up a nice REPL for walking csharp code. However I can't seem to code an implementation of EnvDTE.ProjectItem (interface).

the definition of the misbehaving property on the interface is in indexer as:

string FileNames[short i] {get;}

based on this post I tried

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

which says 'this' in explicit interface declaration is not a member of interface

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

returns Accessor 'UserQuery.ProjectItemFake.this[short].get' cannot implement interface member 'EnvDTE.ProjectItem.get_FileNames(short)' for type 'UserQuery.ProjectItemFake'. Use an explicit interface implementation.

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

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

I'm completely open to .net languages with primary understanding being C#,F#, or VB.net.

can you somehow write an implementation of the interface EnvDTE.ProjectItem in .net?


Solution

  • Just implement the method get_FileNames(short i).

    Properties (including indexed ones) are actually transformed into methods like get_PropertyName and set_PropertyName under the hood. In most cases, the compiler doesn't allow you to implement properties like this, but only because it relies on member metadata. Named indexers aren't available in C#, but they are available in VB.NET, and I think that interface was defined in VB.NET (they are also available in F#, but I'm not sure if they are compatible).

    The C# compiler allows you to implement VB.NET indexed properties as get_PropertyName(Whatever x) because that's the only way to implement them. I tried it and it works for me.

    A full C# implementation is as follows:

    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; }
    }