VB.Net WriteOnly属性到C#

VB.Net WriteOnly属性到C#,c#,vb.net,C#,Vb.net,我在VB.Net中编写了这个类作为自定义控件的一部分。它允许我切换3个链接标签的状态,并使用text属性设置每个标签的文本。这就是用户所能做的一切。我可以毫无疑问地转换Enabled属性和构造函数,但是我不确定转换Text属性的最佳方法。一个函数将包含2个参数,索引器将作用于LabelExtender3,而不是像当前在VB.Net中那样作用于文本。那么,正确的转换方法是什么呢 Public Class LabelExtender3 Private lblTemp(2) As Label

我在VB.Net中编写了这个类作为自定义控件的一部分。它允许我切换3个链接标签的状态,并使用text属性设置每个标签的文本。这就是用户所能做的一切。我可以毫无疑问地转换Enabled属性和构造函数,但是我不确定转换Text属性的最佳方法。一个函数将包含2个参数,索引器将作用于LabelExtender3,而不是像当前在VB.Net中那样作用于文本。那么,正确的转换方法是什么呢

Public Class LabelExtender3
    Private lblTemp(2) As Label

    Public WriteOnly Property Enabled As Boolean
        Set(value As Boolean)
            If value Then
                lblTemp(0).ForeColor = Color.MediumBlue
                lblTemp(1).ForeColor = Color.MediumBlue
                lblTemp(2).ForeColor = Color.MediumBlue
            Else
                lblTemp(0).ForeColor = Color.SteelBlue
                lblTemp(1).ForeColor = Color.SteelBlue
                lblTemp(2).ForeColor = Color.SteelBlue
            End If
        End Set
    End Property

    Public WriteOnly Property Text(ByVal index As Integer) As String
        Set(value As String)
            lblTemp(index).Text = value
        End Set
    End Property

    Friend Sub New(ByRef value1 As Label, ByRef value2 As Label, ByRef value3 As Label)
        lblTemp(0) = value1
        lblTemp(1) = value2
        lblTemp(2) = value3
    End Sub
End Class

您遇到了一个VB.NET具有C#没有的特性:可索引属性

更具体地说,C#缺乏声明它们的能力,但它能够使用它们(这是在C#4.0中添加的),这可能仅限于COM互操作使用

在这种情况下,您最好只制作一个方法:

public void SetText(int index, string value)
{
    lblTemp[index].Text = value;
}
C#不允许带有参数的属性,因此没有直接转换,但您可以只创建一个方法

尝试使用我发现它是转换的最佳工具(我与这家公司没有任何关联):

在C#
中,WriteOnly
属性等同于只包含
集合
部分的C#属性。但是您不能将参数传递给C#属性,除非它是一个索引器,从而成为一个方法。因此,您可以将代码转换为:

public class LabelExtender3
{

    private Label[] lblTemp = new Label[3];
    public bool Enabled
    {
        set
        {
            if (value)
            {
                lblTemp[0].ForeColor = Color.MediumBlue;
                lblTemp[1].ForeColor = Color.MediumBlue;
                lblTemp[2].ForeColor = Color.MediumBlue;
            }
            else
            {
                lblTemp[0].ForeColor = Color.SteelBlue;
                lblTemp[1].ForeColor = Color.SteelBlue;
                lblTemp[2].ForeColor = Color.SteelBlue;
            }
        }
    }

    public string SetText(int index, string value)
    {
        lblTemp[index].Text = value;
    }

    internal LabelExtender3(ref Label value1, ref Label value2, ref Label value3)
    {
        lblTemp[0] = value1;
        lblTemp[1] = value2;
        lblTemp[2] = value3;
    }
}

您没有使用正确的命名约定。@fsacer用户可以使用他们想要的任何命名约定。其次,代码来自对话工具。转换工具可能使用名称
set\u Text
,因为在VB.NET编译时,setter方法在CIL中就是这样命名的
public class LabelExtender3
{

    private Label[] lblTemp = new Label[3];
    public bool Enabled
    {
        set
        {
            if (value)
            {
                lblTemp[0].ForeColor = Color.MediumBlue;
                lblTemp[1].ForeColor = Color.MediumBlue;
                lblTemp[2].ForeColor = Color.MediumBlue;
            }
            else
            {
                lblTemp[0].ForeColor = Color.SteelBlue;
                lblTemp[1].ForeColor = Color.SteelBlue;
                lblTemp[2].ForeColor = Color.SteelBlue;
            }
        }
    }

    public string SetText(int index, string value)
    {
        lblTemp[index].Text = value;
    }

    internal LabelExtender3(ref Label value1, ref Label value2, ref Label value3)
    {
        lblTemp[0] = value1;
        lblTemp[1] = value2;
        lblTemp[2] = value3;
    }
}