博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
根据控件名称反射查找控件
阅读量:7051 次
发布时间:2019-06-28

本文共 1615 字,大约阅读时间需要 5 分钟。

因为对.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;
    }

 

转载地址:http://uesol.baihongyu.com/

你可能感兴趣的文章
------第二节-----------------第二讲----单链表的基本操作---------
查看>>
iOS 百度地图大头针使用
查看>>
1118: 零起点学算法25——求两点之间的距离
查看>>
delegate代理设计模式
查看>>
花10分钟搞懂开源框架吧 - 【NancyFx.Net】
查看>>
busybox的使用
查看>>
GridView(网格视图)+MotionEvent(触控事件)实现可以拖动排序的网格图
查看>>
jq实现全选或者全不选
查看>>
牛人博客
查看>>
linux笔记_20150825_linux有什么好处
查看>>
各种实用工具的使用 学习
查看>>
MarkLight
查看>>
显示/隐藏Mac下的隐藏文件
查看>>
关于数字签名简要原理
查看>>
POJ-3565 Ants 空间点对不相交匹配-最小权值匹配
查看>>
第三次月考
查看>>
单例模式的理解与应用
查看>>
springmvc(一)
查看>>
Hibernate与 MyBatis的比较
查看>>
【51NOD-0】1137 矩阵乘法
查看>>