是否可以为长代码行添加别名?

时间:2015-01-05 12:33:46

标签: c# .net

项目中经常使用以下C#代码:

((DataGridRow) TicketGrid.ItemContainerGenerator.ContainerFromIndex(SelectedRow - 1)).Background = ...;

有没有办法将此别名变成这样的东西?

TicketRow.Background = ...;

1 个答案:

答案 0 :(得分:5)

你可以将它包装在一个方法中:

void SetBackground(TicketGridsType ticketGrid, PropertysType value)
{
    ((DataGridRow) ticketGrid.ItemContainerGenerator.ContainerFromIndex(SelectedRow - 1)).Background = value;
}

然后你会这样打电话:

SetBackground(TicketGrid, ...);

您可以将它包装在一个静态类中,将其变为静态并将签名更改为:

public static void SetBackground(this TicketGridsType ticketGrid, PropertysType value)

然后致电

TicketGrid.SetBackground(...);