我的表单上的某些控件在控件本身上有一个DataSource属性,有些不行。
如果控件具有属性,我想遍历所有控件并将DataSource设置为Nothing。它会像这样工作。
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
我不确定如何在运行时检查这个。
答案 0 :(得分:1)
根据OP的要求,在C#中,使用System.Reflection,您可以执行类似这样的操作来检查类/其实例是否具有属性:
//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
}
检查特定属性:
var prop = props.SingleOrDefault(x => x.Name == "propName");
if(prop != null){
//has that property
//do changing of your Control here
}