Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net WinForms:如何在UserControl中查找所有BindingSources_.net_Winforms_User Controls_Bindingsource - Fatal编程技术网

.net WinForms:如何在UserControl中查找所有BindingSources

.net WinForms:如何在UserControl中查找所有BindingSources,.net,winforms,user-controls,bindingsource,.net,Winforms,User Controls,Bindingsource,在我们正在开发的程序中,用户数据收集在UserControls中,UserControls是使用BindingSources绑定到业务实体的数据 我需要以编程方式查找UserControl中的所有BindingSources 由于BindingSource源未添加到UserControl的控件集合,因此我无法在其中搜索 可以这样做吗?BindingSource是一个组件,而不是控件,因此确实在控件集合中找不到它。但是,使用设计器添加组件时,它会创建一个名为components的IContaine

在我们正在开发的程序中,用户数据收集在UserControls中,UserControls是使用BindingSources绑定到业务实体的数据

我需要以编程方式查找UserControl中的所有BindingSources

由于BindingSource源未添加到UserControl的控件集合,因此我无法在其中搜索


可以这样做吗?

BindingSource
是一个
组件,而不是
控件
,因此确实在
控件
集合中找不到它。但是,使用设计器添加组件时,它会创建一个名为
components
IContainer
类型的字段,并将组件添加到该字段中。该字段是私有的,因此只能从声明该字段的类中访问它(除非使用反射)

我认为实现所需的最简单方法是向所有使用控件添加
GetBindingSources
方法:

public IEnumerable<BindingSource> GetBindingSources()
{
    return components.Components.OfType<BindingSource>();
}
public IEnumerable GetBindingSources()
{
返回components.components.OfType();
}

当然,它只适用于使用设计器创建的
BindingSources
,而不适用于您动态创建的源代码(除非您将它们添加到容器)

最大的问题是为我的方法找到一个可用于所有用户控件的解决方案,并且仍然能够使用Visual Studio中的WinForms designer

因为我不知道如何在非从UserControl派生的类上使用设计器,所以我制作了一个没有任何方法的接口,IBusinessEntityEditorView,以及一个采用这种视图的扩展方法,使用反射来查找我在其中搜索BindingSources的components字段:

public interface IBusinessEntityEditorViewBase
{
}

...

public static void EndEditOnBindingSources(this IBusinessEntityEditorViewBase view)
{
    UserControl userControl = view as UserControl;
    if (userControl == null) return;

    FieldInfo fi = userControl.GetType().GetField("components", BindingFlags.NonPublic | BindingFlags.Instance);
    if (fi != null)
    {
        object components = fi.GetValue(userControl);
        if (components != null)
        {
            IContainer container = components as IContainer;
            if (container != null)
            {
                foreach (var bindingSource in container.Components.OfType<BindingSource>())
                {
                    bindingSource.EndEdit();
                }
            }
        }
    }
}
公共接口IBusinessEntityEditorViewBase
{
}
...
公共静态void EndeditionBindingSources(此IBusinessEntityEditorViewBase视图)
{
UserControl UserControl=作为UserControl查看;
if(userControl==null)返回;
FieldInfo fi=userControl.GetType().GetField(“组件”,BindingFlags.NonPublic | BindingFlags.Instance);
如果(fi!=null)
{
对象组件=fi.GetValue(userControl);
if(组件!=null)
{
IContainer容器=作为IContainer的组件;
if(容器!=null)
{
foreach(container.Components.OfType()中的var bindingSource)
{
bindingSource.EndEdit();
}
}
}
}
}

我选择使用反射,因为我不知道如何在一个不是直接从UserControl派生的类上使用VS中的WinForms designer,我需要这个方法来访问我的所有UserControl。我的解决方案是让UserControls实现一个接口IBusinessEntityEditorView,并为该接口创建一个扩展方法来获取所有BindingSources。