我正在尝试为行走的csharp代码设置一个不错的REPL
。但是,我似乎无法编写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";}}
返回 Accessor'UserQuery.ProjectItemFake.this [short] .get'无法为类型'UserQuery.ProjectItemFake'实现接口成员'EnvDTE.ProjectItem.get_FileNames(short)'。使用显式接口实现。
[IndexerName("FileNames")]
string ProjectItem.this[short i] {get{return "test";}}
在显式接口声明中返回'this'不是接口的成员
我对.net语言完全开放,主要理解为C#,F#或VB.net。
你能以某种方式在.net?
中编写接口EnvDTE.ProjectItem
的实现
答案 0 :(得分:3)
只需实施方法get_FileNames(short i)
即可。
属性(包括索引的属性)实际上被转换为诸如get_PropertyName
和set_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; }
}
答案 1 :(得分:0)
我怀疑Greg是正确的,因为类似的方法适用于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
}