简体   繁体   English

如何在C#中使用Reflection强制转换对象运行时?

[英]How to cast an object Run-time using Reflection in C#?


i am developing a windows application and in that i created a class for common look and feel setting 我正在开发Windows应用程序,因此我创建了一个用于常见外观设置的类

CommonAppearance class Code : 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 ? UseCommonTheme(Form form)而不是使用switch case我们可以使用反射来投射控件吗? somthing like

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


Thanks in advance. 提前致谢。

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 });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM