There are two different UserControls which share some common Properties. What I d like to do is to switch between these two based on an external flag.
UserControl u1, u2;
if(flag)
{
u1 = u1 as ControlType1;
u2 = u2 as ControlType1;
}
else
{
u1 = u1 as ControlType2;
u2 = u2 as ControlType2;
}
SomeMethod(u1.SelectedItemName, u2.SelectedItemName);
由于用户控制没有名为“ 选中项目” 的属性, 代码不会丢弃错误 。
我目前所做的是,我添加了一个用户控制扩展方法, 使用反射获得“ 选中项目名称 ”, 我通过调用 u1. 选中项目名称( ) 而不是 u1. 选中项目 来获得此值 ;
我的问题是,在不使用扩展/也许正确的方式的情况下,什么是解决问题的简单方法。 请注意,我不想在声明(a,b)中重复SomeMethod (a,b) 。