【问题标题】:Finding all DataSets on a Windows/DevX Form查找 Windows/DevX 窗体上的所有数据集
【发布时间】:2017-07-14 12:48:41
【问题描述】:

我想问是否有可能以某种方式找到并列出表单上的所有数据集。

我在表单控件中找不到它们,它们是通过 Visual Studio 表单设计器添加的,我正在构建的软件中会有很多不同的数据集,我想为它们的一般管理编写一个库,但是为此,我必须以某种方式将它们添加到列表中,但我找不到有关此主题的任何内容。

List<DataSet> formSets = new List<DataSet>();
  //Operation to find all DataSets on the Form <--- This is what I'm looking for,
  //probably a cycle which results in DataSet typed foundDataSet each time it executes.
formSets.Add(foundDataSet)
  //Number of other initializing operations like setting defaults and so on.

DataSet 是强类型的,但我的目标只是对它们执行通用的 DataSet 操作,正如代码已经说明的那样。

提前谢谢你,

格雷/加里 H.

【问题讨论】:

  • 如果查看生成的 exe 或 dll,数据集是在设计的类中还是在部分表单的类中都没有关系。只需找到第一个表单,然后递归迭代 Controls 属性,查看控件是否继承自 Dataset
  • 你能举个例子吗?我似乎无法通过代码中的表单控件列表访问数据集。

标签: c# winforms strongly-typed-dataset


【解决方案1】:

好吧,经过一番思考,我意识到 Dataset 的实例不是控件集合的一部分,它们只是表单类的成员。 因此,想法是加载包含程序集,获取所有 Form 的子类,然后找到它们的所有数据集类型或继承自它的成员。下面的代码应该使用反射来实现

using System;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace TypeFinder
{
    class Program
    {
        static void Main(string[] args)
        {
            // args[0]: Assembly path, args[1] Assembly containing type to find, args[2] Type to find
            Type typeToFind = LoadTypeFrom(args[1], args[2]);
            var forms = Assembly.LoadFrom(args[0]).GetTypes().Where(t => t.IsSubclassOf(typeof(Form)));
            foreach (Type form in forms)
            {
                var fields = form.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
                foreach (FieldInfo fieldInfo in fields)
                {
                    if (IsSubclassOrSameTypeAs(typeToFind, fieldInfo.FieldType))
                    {
                        Console.Out.WriteLine($"Found type {fieldInfo.FieldType} as IsSubclassOrSameTypeAs of {typeToFind}");
                    }
                }
            }
            Console.Out.WriteLine("Press any key to exit...");
            Console.ReadLine();
        }

        private static bool IsSubclassOrSameTypeAs(Type baseType, Type descendant)
        {
            return descendant.IsSubclassOf(baseType) || descendant == baseType;
        }


        private static Type LoadTypeFrom(string path, string type)
        {
            if (string.IsNullOrEmpty(path))
            {
                return Type.GetType(type, true, true);
            }
            var assembly = Assembly.LoadFrom(path);
            return assembly.GetType(type, true, true);
        }

    }
}

您可以使用程序集路径或完全限定的类型名称来调用此程序:

"C:\MyCustomAssembly.dll" "C:\MyCustomAssemblyWithTypeToFind.dll" "MyNamespace.MyCustomType"

"C:\MyCustomAssembly.dll" "" "System.Data.DataSet, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

希望这有帮助,亲切的问候。

【讨论】:

    猜你喜欢
    • 2013-10-20
    • 2015-09-29
    • 2021-08-18
    • 1970-01-01
    • 1970-01-01
    • 2015-08-08
    • 2013-06-14
    • 2019-06-29
    相关资源
    最近更新 更多