zoukankan      html  css  js  c++  java
  • 根据控件名称反射查找控件

    因为对.net了解不是太深入,所以只能做出这样的水平:

    找到要查找的反射属性信息:

        PropertyInfo^ getPropertyInfo(Type^ t, String^ pName) {
            PropertyInfo^ pInfo;
            while (t != nullptr) {
                pInfo = t->GetProperty(pName, BindingFlags::DeclaredOnly | BindingFlags::Public | BindingFlags::Instance);
                if (pInfo != nullptr)
                {
                    return pInfo;
                }
                t = t->BaseType;
            }
            return nullptr;
        }


    从一个Component开始查找,然后查找其子Component是否有名为compName的控件,有则返回,无则返回nullptr

        // get a component by it's name, the component is in comp
        Component^ getComponentByName(String^ compName, Component^ comp) {
            if (nullptr == comp)
            {
                return comp;
            }

            // if this component is the right one, then return it
            Type^ t = comp->GetType();
            PropertyInfo^ pInfo = t->GetProperty("Name");
            if (pInfo != nullptr && compName->Equals(dynamic_cast<String^>(pInfo->GetValue(comp, nullptr))))
            {
                return comp;
            }

            // search this component's children Controls
            Component^ retComp;
            pInfo = getPropertyInfo(t, "Controls");
            if (pInfo != nullptr)
            {
                System::Collections::IList^ list = safe_cast<System::Collections::IList^>(pInfo->GetValue(comp, nullptr));
                if (list != nullptr)
                {
                    for (int i = 0; i < list->Count; i++)
                    {
                        if (nullptr != (retComp = getComponentByName(compName, safe_cast<Component^>(list[i]))))
                        {
                            return retComp;
                        }
                    }
                }
            }

            // search this component's children Items
            pInfo = getPropertyInfo(t, "Items");
            if (pInfo != nullptr)
            {
                System::Collections::IList^ list = safe_cast<System::Collections::IList^>(pInfo->GetValue(comp, nullptr));
                if (list != nullptr)
                {
                    for (int i = 0; i < list->Count; i++)
                    {
                        if (nullptr != (retComp = getComponentByName(compName, safe_cast<Component^>(list[i]))))
                        {
                            return retComp;
                        }
                    }
                }
            }

            return nullptr;
        }

  • 相关阅读:
    (二)《机器学习》(周志华)第4章 决策树 笔记 理论及实现——“西瓜树”——CART决策树
    (一)《机器学习》(周志华)第4章 决策树 笔记 理论及实现——“西瓜树”
    python学习笔记——递归算法
    Python爬虫抓取东方财富网股票数据并实现MySQL数据库存储
    MySQL安装总是失败,提示缺少Visual Studio 2013 Redistributable
    Python爬虫简单实现之Q乐园图片下载
    Python爬虫之简单的图片获取
    Springboot启动,URL正确,但是报404
    Linux常用命令
    MYSQL常用的配置参数优化
  • 原文地址:https://www.cnblogs.com/gc2013/p/3878028.html
Copyright © 2011-2022 走看看