I am trying get a Form’s BindingSource using Reflection. The following code is what I’ve tried so far although it has an error:
public class MyClass :Form
{
BindingSource bs = new BindingSource();
}
public static class Class2
{
public static BindingSource GetBindingSource(string FieldNameP, Form FormP)
{
BindingSource Bs = null;
var info=FormP.GetType().GetField(FieldNameP);
if(info != null)
{
Bs = (BindingSource)info.GetValue(null)
}
return Bs;
}
}
On your call to get field, you need to use the overload that accepts binding flags. You may need to trial and error it a bit but I think your need:
.GetValuetakes the instance of the object you want to invoke the field on.A better approach may be to create an interface:
Apply it to the form:
Use the interface: