locked
Retrieving PROPERTY VALUE Dynamically RRS feed

  • Question

  •  Hi,

    I want retrieve the properties value dynamically. I mean for a function I will be passing any CONTROL OBJECT as one parameter and property  value  as another parameter. Depending upon the property it should return the value

    FUNCTION---- string   Function(Control Obj,string property);

    Function(ButtonObj,"ID");
    Function(ButtonObj,"Text");
    Function(DropDownObject,"SelectedText")
    Depending Upon second parameter Button should return Buttonobj.ID, Buttonobj.Text and Dropdown should return DropDownObject.SelectedText
    respectively.
    I am not getting idea how to do this one.Please help me

    Its Urgent
    Thanks in advance

    Friday, October 31, 2008 2:31 PM

Answers

  • You would want to use the classes from the System.Reflection namespace to accomplish this:

    public string GetValue(System.Windows.Forms.Control obj, string property)  
    {  
        object value = obj.GetType().GetProperty(property, BindingFlags.Public | BindingFlags.Instance).GetValue(obj, null);  
        if (value != null)  
            return value.ToString();  
        return string.Empty;  

    David Morton - http://blog.davemorton.net/
    • Marked as answer by Harry Zhu Tuesday, November 4, 2008 2:51 AM
    Friday, October 31, 2008 2:45 PM

All replies

  • You would want to use the classes from the System.Reflection namespace to accomplish this:

    public string GetValue(System.Windows.Forms.Control obj, string property)  
    {  
        object value = obj.GetType().GetProperty(property, BindingFlags.Public | BindingFlags.Instance).GetValue(obj, null);  
        if (value != null)  
            return value.ToString();  
        return string.Empty;  

    David Morton - http://blog.davemorton.net/
    • Marked as answer by Harry Zhu Tuesday, November 4, 2008 2:51 AM
    Friday, October 31, 2008 2:45 PM
  • Hi David,

    Thanks for ur help.
    It works

    Monday, November 3, 2008 12:15 PM