Is there any way to create a function in C# that can change the visibility of an given object that has the visibility property?

```
public void changeVisibilityState(Button test)
{
    if (test.Visibility == Visibility.Collapsed)
    {
        test.Visibility = Visibility.Visible;
    }
    else
    {
        test.Visibility = Visibility.Collapsed;
    }
}

```

Like this but for any object with the visibility property.