如何在C#中使用Reflection运行对象运行时?

时间:2022-01-31 03:12:29


i am developing a windows application and in that i created a class for common look and feel setting

CommonAppearance class Code :

我正在开发一个Windows应用程序,并在其中创建了一个用于常见外观设置的类CommonAppearance类代码:

static void SetCommonAppearance(Label ctrl){ //some appearance setting code}
static void SetCommonAppearance(GridGroupingControl ctrl){ //some appearance setting code}
static void SetCommonAppearance(GradientPanel ctrl){ //some appearance setting code}
static void SetCommonAppearance(Form ctrl){ //some appearance setting code}
static void SetCommonAppearance(ComboBox ctrl){ //some appearance setting code}
static void SetCommonAppearance(CheckBox ctrl){ //some appearance setting code}
static void SetCommonAppearance(RadioButton ctrl){ //some appearance setting code}
static void SetCommonAppearance(Button ctrl){ //some appearance setting code}

public static void UseCommonTheme(Form form)
{
    List<Control> lstControls = GetAllControls(form.Controls);
    foreach (Control ctr in lstControls)
    {
       string temp2 = ctr.GetType().Name;
       switch (temp2)
       {
          case "TextBox":
               SetCommonAppearance((TextBox)ctr);
               break;
          case "AutoLabel":
               SetCommonAppearance((Label)ctr);
               break;
          case "GridGroupingControl":
               SetCommonAppearance((GridGroupingControl)ctr);
               break;
          case "ButtonAdv":
               ApplyCustomTheme((ButtonAdv)ctr);
               break;
          case "CheckBoxAdv":
               SetCommonAppearance((CheckBox)ctr);
               break;
          case "ComboBoxAdv":
               SetCommonAppearance((ComboBox)ctr);
               break;
          case "RadioButtonAdv":
               SetCommonAppearance((RadioButton)ctr);
               break;
       }

    }
}

this is acceptable when there are less control application to set common appearance but in my application there are number of different type of controls are used.
In the method UseCommonTheme(Form form) instead of using switch case Can we use reflection to cast the controls ? somthing like

当设置共同外观的控制应用程序较少时,这是可以接受的,但在我的应用程序中,使用了许多不同类型的控件。在方法UseCommonTheme(表单形式)而不是使用switch case我们可以使用反射来转换控件吗?有点像

foreach (Control ctr in lstControls)
{
    string controlType = ctr.GetType().Name;
    SetCommonAppearance((class reference of 'controlType')ctr);
}


Thanks in advance.

提前致谢。

1 个解决方案

#1


3  

If you are using .net 4 you can take advantage of the dlr (dynamic language runtime):

如果您使用的是.net 4,则可以利用dlr(动态语言运行时):

foreach (dynamic ctr in lstControls)
{
    SetCommonAppearance(ctr);
}

The dlr will resolve the correct overload for you.

dlr将为您解决正确的过载问题。

If you want to use reflection:

如果你想使用反射:

var type = typeof(CommonAppearance);
var methods = type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
foreach (Control ctr in lstControls)
{
   var setAppearanceMethod = 
        methods.FirstOrDefault(m => m.GetParameters()[0].ParameterType == ctr.GetType());
   if(setAppearanceMethod!=null)
       setAppearanceMethod.Invoke(null, new[] { ctr });
}

#1


3  

If you are using .net 4 you can take advantage of the dlr (dynamic language runtime):

如果您使用的是.net 4,则可以利用dlr(动态语言运行时):

foreach (dynamic ctr in lstControls)
{
    SetCommonAppearance(ctr);
}

The dlr will resolve the correct overload for you.

dlr将为您解决正确的过载问题。

If you want to use reflection:

如果你想使用反射:

var type = typeof(CommonAppearance);
var methods = type.GetMethods(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
foreach (Control ctr in lstControls)
{
   var setAppearanceMethod = 
        methods.FirstOrDefault(m => m.GetParameters()[0].ParameterType == ctr.GetType());
   if(setAppearanceMethod!=null)
       setAppearanceMethod.Invoke(null, new[] { ctr });
}