首页 > 学院 > 开发设计 > 正文

根据控件名称反射查找控件

2019-11-17 03:00:12
字体:
来源:转载
供稿:网友
根据控件名称反射查找控件

因为对.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){returnpInfo;}t=t->BaseType;}returnnullptr;}

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

//getacomponentbyit'sname,thecomponentisincompComponent^getComponentByName(String^compName,Component^comp){if(nullptr==comp){returncomp;}//ifthiscomponentistherightone,thenreturnitType^t=comp->GetType();PropertyInfo^pInfo=t->GetProperty("Name");if(pInfo!=nullptr&&compName->Equals(dynamic_cast<String^>(pInfo->GetValue(comp,nullptr)))){returncomp;}//searchthiscomponent'schildrenControlsComponent^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(inti=0;i<list->Count;i++){if(nullptr!=(retComp=getComponentByName(compName,safe_cast<Component^>(list[i])))){returnretComp;}}}}//searchthiscomponent'schildrenItemspInfo=getPropertyInfo(t,"Items");if(pInfo!=nullptr){System::Collections::IList^list=safe_cast<System::Collections::IList^>(pInfo->GetValue(comp,nullptr));if(list!=nullptr){for(inti=0;i<list->Count;i++){if(nullptr!=(retComp=getComponentByName(compName,safe_cast<Component^>(list[i])))){returnretComp;}}}}returnnullptr;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表