因为对.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;}
新闻热点
疑难解答