日期:2014-05-20 浏览次数:21076 次
public static class ExtensionControl
{                                               
    public static object GetPropertySafe(this Control control, string propertyName)
    {
        object returnValue = null;
        Action func = () =>
        {
            Type type = control.GetType();
            returnValue = type.InvokeMember(propertyName, BindingFlags.GetProperty, null, control, null);
        };
        if (control.InvokeRequired)
        {
            control.Invoke(func);
        }
        else
        {
            func();
        }
        return returnValue;
    }
    public static object SetPropertySafe(this Control control, string propertyName, object value)
    {
        object returnValue = null;
        Action func = () =>
        {
            Type type = control.GetType();
            returnValue = type.InvokeMember(propertyName, BindingFlags.SetProperty, null, control, new object[] { value });
        };
        if (control.InvokeRequired)
        {
            control.Invoke(func);
        }
        else
        {
            func();
        }
        return returnValue;
    }
    public static object GetPropertySafe(this ToolStripMenuItem control, string propertyName)
    {
        object returnValue = null;
        Control owner = control.Owner;
        Action func = () =>
        {
            Type type = control.GetType();
            returnValue = type.InvokeMember(propertyName, BindingFlags.GetProperty, null, control, null);
        };
        if (owner.InvokeRequired)
        {
            owner.Invoke(func);
        }
        else
        {
            func();
        }
        return returnValue;
    }
    public static object SetPropertySafe(this ToolStripMenuItem control, string propertyName, object value)
    {
        object returnValue = null;
        Control owner = control.Owner;
        Action func = () =>
        {
            Type type = control.GetType();
            returnValue = type.InvokeMember(propertyName, BindingFlags.SetProperty, null, control, new object[] { value });                
        };
        if (owner.InvokeRequired)
        {
            owner.Invoke(func);
        }
        else
        {
            func();
        }
        return returnValue;
    }        
    public static object InvokeMethodSafe(this Control control, string methodName, params object[] args)
    {
        object returnValue = null;
        if (args == null)
        {
            args = new object[1];
            args[0] = null;
        }
        else if (args != null && args.Length == 0)
        {
            args = null;
        }
        Action func = () =>
        {
            Type type = control.GetType();                
            returnValue = type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, control, args);
        };
        if (control.InvokeRequired)
        {
            control.Invoke(func);
        }
        else
        {
            func();
        }
        return returnValue;
    }
    public static object InvokeMethodSafe(this ToolStripMenuItem control, string methodName, params object[] args)
    {
        object returnValue = null;
        if (args == null)
        {
            args = new object[1];
            args[0] = null;
        }
        else if (args != null && args.Length == 0)
        {
            args = null;
        }
        Control owner = control.Owner;
        Action func = () =>
        {
            Type type = control.GetType();
            returnValue = type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, control, args);
        };
        if (owner.InvokeRequired)
        {
            owner.Invoke(func);
        }
        else
        {
            func();
        }
        return returnValue;
    }
}