通配符是否可以处理对象?

New*_*Bie 0 c#

是否可以在参数中使用通配符?我有这个代码重复的属性TextLine1,TextLine2,TextLine3和TextLine 4.是否可以用通配符替换数字,以便我可以根据用户输入传递数字.

TextLine1,TextLine2,TextLine3和TextLine 4是ReportHeader类的属性.

 public Control returnTextLine(ReportHeader TextLineObj,int i)
    {

        System.Windows.Forms.Label lblTextLine = new System.Windows.Forms.Label();
        lblTextLine.Name = TextLineObj.**TextLine1**.Name;
        lblTextLine.Font = TextLineObj.**TextLine1**.Font;
        lblTextLine.ForeColor = TextLineObj.**TextLine1**.ForeColor;
        lblTextLine.BackColor = TextLineObj.**TextLine1**.BackgroundColor;
        lblTextLine.Text = TextLineObj.**TextLine1**.Text;
        int x = TextLineObj.**TextLine1**.x;
        int y = TextLineObj.**TextLine1**.y;
        lblTextLine.Location = new Point(x, y);


        return lblTextLine;
    }
Run Code Online (Sandbox Code Playgroud)

请帮忙...

Dan*_*rth 5

不,这不可能.
但是,您可以TextLineObj使用以下TextLines属性扩展表示的类ReadonlyCollection:

public class TextLineObj
{
    public ReadonlyCollection<TextLine> TextLines { get; private set; }

    public TextLineObj()
    {
        TextLines = new ReadonlyCollection<TextLine>(
                            new List<TextLine> { TextLine1, TextLine2, 
                                                 TextLine3, TextLine4 });
    }
}
Run Code Online (Sandbox Code Playgroud)

像这样使用它:

TextLineObj.TextLines[i].Name;
Run Code Online (Sandbox Code Playgroud)