【问题标题】:Get param and element by strings通过字符串获取参数和元素
【发布时间】:2013-03-12 22:21:44
【问题描述】:

如何在c#中实现这个方法:

public static void SetParam(string element, string property, dynamic value){
 // Do something
}

// Usage:
setParam("textBox1","Text","Hello");

在 JavaScript 中是这样的:

function SetParam(element, property, value) {
 document.getElementById(element)[property]=value;
}

// Usage:
SetParam("textBox","value","Hello");

【问题讨论】:

  • C#代码中的“元素”是什么意思?
  • 如果您在将值传递给单独的方法时知道元素和属性,为什么不能在代码隐藏中使用 textbox1.Text = value; 而不是调用单独的方法来设置值。
  • 我需要使用三个已知字符串从方法中设置属性值:元素(ElementID,例如 texbox1)、属性(例如 Text)和值。

标签: c# properties parameters get


【解决方案1】:

也许以下对你有用。

public void SetParam(string element, string property, dynamic value)
{
    FieldInfo field = typeof(Form1).GetField(element, BindingFlags.NonPublic | BindingFlags.Instance);
    object control = field.GetValue(this);
    control.GetType().GetProperty(property).SetValue(control, value, null);
}

Form1 替换为包含您要修改的控件的表单类。

编辑:在阅读了 Blachshma 的回答后,我意识到你必须把

using System.Reflection;

在文件的顶部。

我还假设它用于 Windows 窗体应用程序。

最后,获取控件引用的更好方法可能是像 Greg 建议的那样使用 Form.Controls 属性。

【讨论】:

    【解决方案2】:

    如果我正确理解您的问题,这可以在 Reflection 的帮助下完成 ...

    首先在您的 cs 文件顶部添加:using System.Reflection;

    由于我不知道您使用的是 WPF 还是 Winforms - 这里有 2 个示例...
    WPF:

    您可以使用此版本的 SetParam:

    private void SetParam(string name, string property, dynamic value)
    {
          // Find the object based on it's name
          object target = this.FindName(name);
    
          if (target != null)
          {
              // Find the correct property
              Type type = target.GetType();
              PropertyInfo prop = type.GetProperty(property);
    
              // Change the value of the property
              prop.SetValue(target, value);
          }
    }
    

    用法:

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
       SetParam("textbox", "Text", "Hello");   
    

    textbox 声明如下:

    <TextBox x:Name="textbox" />
    

    对于 Winforms,只需将 SetParam 更改为:

    private void SetParam(string name, string property, dynamic value)
    {
          // Find the object based on it's name
          object target = this.Controls.Cast<Control>().FirstOrDefault(c => c.Name == name);
    
          if (target != null)
          {
              // Find the correct property
              Type type = target.GetType();
              PropertyInfo prop = type.GetProperty(property);
    
              // Change the value of the property
              prop.SetValue(target, value);
          }
    }
    

    【讨论】:

      【解决方案3】:

      假设“元素”变量是控件的 Id 然后使用反射:

          

      PropertyInfo propertyInfo = form1.Controls.Where(c => c.id == element).FirstOrDefault().GetType().GetProperty(property,
                                  BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase);
          if (propertyInfo != null)
          {
              if (propertyInfo.PropertyType.Equals(value.GetType()))
                  propertyInfo.SetValue(control, value, null);
              else
                  throw new Exception("Property DataType mismatch, expecting a " +
                                      propertyInfo.PropertyType.ToString() + " and got a " +
                                      value.GetType().ToString());
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-20
        • 1970-01-01
        • 1970-01-01
        • 2018-01-21
        • 1970-01-01
        • 2020-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多