Jos*_*eph 1 c# vb.net properties
我发现以下语法作为VB.NET属性,我试图将其转换为c#,但我不知道如何实现.
Public Property SomeText(ByVal someEnumThing as SomeEnum) As String
Get
Select Case someEnumThing
//figure out what string to return
End Select
End Get
Set(ByVal Value as String)
Select Case someEnumThing
//figure out what string to set
End Select
End Set
End Property
Run Code Online (Sandbox Code Playgroud)
我以前从未见过这样的房产,有什么想法吗?
Luc*_*ero 12
我想你是指这个属性的论据.好吧,据我所知,C#只支持索引器,它们没有名称(例如this[SomeEnum someEnumThing] {}).
如果要获得类似的行为,可以创建一个带有索引器属性的帮助器类,并使用它来公开属性的"名称":
public class YourClass {
public struct SomeTextProperty {
private readonly YourClass owner;
internal SomeTextProperty(YourClass owner) {
this.owner = owner;
}
public string this[SomeEnum someEnumThing] {
get {
return owner.GetSomeText(someEnumThing);
}
set {
owner.SetSomeText(someEnumThing, value);
}
}
}
public SomeTextProperty SomeText {
get {
return new SomeTextProperty(this);
}
}
private string GetSomeText(SomeEnum someEnumThing) {
// implementation to get it
}
private void SetSomeText(SomeEnum someEnumThing, string value) {
// implementation to set it
}
}
Run Code Online (Sandbox Code Playgroud)