问题描述
这是我在msdn上看到的:usingSystem;usingSystem.Reflection;usingSystem.Security;publicclassMyFilterAttributeSample{publicstaticvoidMain(){try{MemberFiltermyFilter=Type.FilterAttribute;TypemyType=typeof(System.String);MemberInfo[]myMemberInfoArray=myType.FindMembers(MemberTypes.Constructor|MemberTypes.Method,BindingFlags.Public|BindingFlags.Static|BindingFlags.Instance,myFilter,MethodAttributes.SpecialName);foreach(MemberInfomyMemberinfoinmyMemberInfoArray){Console.Write("n"+myMemberinfo.Name);Console.Write("isa"+myMemberinfo.MemberType.ToString());}}catch(ArgumentNullExceptione){Console.Write("ArgumentNullException:"+e.Message);}catch(SecurityExceptione){Console.Write("SecurityException:"+e.Message);}catch(Exceptione){Console.Write("Exception:"+e.Message);}}}C++复制代码usingnamespaceSystem;usingnamespaceSystem::Collections;usingnamespaceSystem::Reflection;usingnamespaceSystem::Security;intmain(){try{MemberFilter^myFilter=Type::FilterAttribute;Type^myType=System::String::typeid;array<MemberInfo^>^myMemberInfoArray=myType->FindMembers(static_cast<MemberTypes>(MemberTypes::Constructor|MemberTypes::Method),static_cast<BindingFlags>(BindingFlags::Public|BindingFlags::Static|BindingFlags::Instance),myFilter,MethodAttributes::SpecialName);IEnumerator^myEnum=myMemberInfoArray->GetEnumerator();while(myEnum->MoveNext()){MemberInfo^myMemberinfo=safe_cast<MemberInfo^>(myEnum->Current);Console::Write("n{0}",myMemberinfo->Name);Console::Write("isa{0}",myMemberinfo->MemberType);}}catch(ArgumentNullException^e){Console::Write("ArgumentNullException:{0}",e->Message);}catch(SecurityException^e){Console::Write("SecurityException:{0}",e->Message);}catch(Exception^e){Console::Write("Exception:{0}",e->Message);}}请问“这个程序是怎么实现这句话的:示例获取FilterAttribute委托,将其作为参数传递给FindMembers方法,然后显示指定的成员和它们的属性。我对这句有些不明白能解释一下吗?谢谢
解决方案
解决方案二:
看一下这个例子usingSystem;usingSystem.Reflection;classMyFindMembersClass{publicstaticvoidMain(){ObjectobjTest=newObject();TypeobjType=objTest.GetType();MemberInfo[]arrayMemberInfo;try{//FindallstaticorpublicmethodsintheObjectclassthatmatchthespecifiedname.arrayMemberInfo=objType.FindMembers(MemberTypes.Method,BindingFlags.Public|BindingFlags.Static|BindingFlags.Instance,newMemberFilter(DelegateToSearchCriteria),"ReferenceEquals");for(intindex=0;index<arrayMemberInfo.Length;index++)Console.WriteLine("ResultofFindMembers-t"+arrayMemberInfo[index].ToString()+"n");}catch(Exceptione){Console.WriteLine("Exception:"+e.ToString());}}publicstaticboolDelegateToSearchCriteria(MemberInfoobjMemberInfo,ObjectobjSearch){//Comparethenameofthememberfunctionwiththefiltercriteria.if(objMemberInfo.Name.ToString()==objSearch.ToString())returntrue;elsereturnfalse;}}