1

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?

Community
  • 1
  • 1
Maslow
  • 18,464
  • 20
  • 106
  • 193

2 Answers2

3

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; }
}
Maslow
  • 18,464
  • 20
  • 106
  • 193
GregRos
  • 8,667
  • 3
  • 37
  • 63
0

I suspect Greg is correct as a similar approach worked in F#

Here's the an entire interface implementation that seems to be working fine.

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
    }
Community
  • 1
  • 1
Maslow
  • 18,464
  • 20
  • 106
  • 193