2013-03-12 73 views
0

如何實現在C#中此方法:獲取PARAM和元素的字符串

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"); 
+0

這是在C#代碼「元件」的含義是什麼? – 2013-03-12 22:23:21

+0

如果在將值傳遞給單獨的方法時知道元素和屬性,爲什麼不能在代碼隱藏中執行'textbox1.Text = value;'而不是調用單獨的方法來設置值。 – 2013-03-12 22:26:54

+0

我需要使用三個已知字符串的方法設置屬性值:元素(ElementID,如texbox1),屬性(如文本)和值。 – 2013-03-12 22:35:54

回答

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屬性。

+0

非常感謝! – 2013-03-13 10:02:20

1

如果我正確理解你的問題,這可能與一些幫助來完成Reflection ...

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

因爲我不知道,如果你使用WPF或的WinForms - 這裏有兩個例子...
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); 
     } 
} 
+0

非常感謝! – 2013-03-13 10:02:00

0

假設「元件」的變量是我的控制的,然後使用反射:

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