Reputation: 320
Some controls on my form have a DataSource property on the control itself, some dont.
I want to loop through all the controls and set the DataSource to Nothing if the control has the property. It would work something like this.
Private Sub ClearAllDatabindings()
If _dataBindingsSet = True Then
For Each ctrl As Control In Me.Controls
ClearDataBindings(ctrl)
SetDatasourceToNothing(ctrl) '-- This is the piece idk how to Write.
Next
End If
End Sub
I am not sure how to check this during run-time.
Upvotes: 1
Views: 261
Reputation: 30823
As requested by OP, In C#, using System.Reflection, you could do something like this to check if a class / its instance has property or not:
//for class type
var props = typeof(MyClass).GetProperties();
if (props == null || props.Length <= 0) { //does not have property
//do something
}
//for class instance
var props = classInstance.GetType().GetProperties();
if (props == null || props.Length <= 0) { //does not have property
//do something
}
To check for specific property:
var prop = props.SingleOrDefault(x => x.Name == "propName");
if(prop != null){
//has that property
//do changing of your Control here
}
Upvotes: 1