dimanche 26 mai 2019

C# reflection datatype

I would like to optimize the below code The only difference is the datatypes RadioButton, Label, and Button. Outside the method I have an loop that iterates over all the controls in the page.

if (control is RadioButton)
{
    try
    {
        (control as RadioButton).Text = SPContext.Current.Web.Locale.LCID == 1033 ?
        DataBinder.Eval(keys.en, control.ID).ToString() :
        DataBinder.Eval(keys.sv, control.ID).ToString();
    }
    catch (Exception)
    {
        (control as RadioButton).Text = "Key not found: " + control.ID;
    }
}
else if (control is Label)
{
    try
    {
        (control as Label).Text = SPContext.Current.Web.Locale.LCID == 1033 ?
        DataBinder.Eval(keys.en, control.ID).ToString() :
        DataBinder.Eval(keys.sv, control.ID).ToString();
    }
    catch (Exception)
    {
        (control as RadioButton).Text = "Key not found: " + control.ID;
    }
}
else if (control is Button)
{
    try
    {
        (control as Button).Text = SPContext.Current.Web.Locale.LCID == 1033 ?
        DataBinder.Eval(keys.en, control.ID).ToString() :
        DataBinder.Eval(keys.sv, control.ID).ToString();
    }
    catch (Exception)
    {
        (control as RadioButton).Text = "Key not found: " + control.ID;
    }
}





Aucun commentaire:

Enregistrer un commentaire