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

王朝学院·作者佚名  2016-05-17
窄屏简体版  字體: 小  |  中  |  大  |  超大  

根据控件名称反射查找控件因为对.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;}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航