Mega Code Archive

 
Categories / C# / Reflection
 

Returns an array of Type objects representing a filtered list of interfaces implemented or inherited by the current Type

using System; using System.Xml; using System.Reflection; public class MyFindInterfacesSample  {     public static void Main()     {             XmlDocument myXMLDoc = new XmlDocument();             myXMLDoc.LoadXml("<book genre='novel' ISBN='1-111111-11-1'>" +                 "<title>C#</title>" + "</book>");             Type myType = myXMLDoc.GetType();             TypeFilter myFilter = new TypeFilter(MyInterfaceFilter);             String[] myInterfaceList = new String[2]                  {"System.Collections.IEnumerable",                  "System.Collections.ICollection"};             for(int index=0; index < myInterfaceList.Length; index++)             {                 Type[] myInterfaces = myType.FindInterfaces(myFilter, myInterfaceList[index]);                 if (myInterfaces.Length > 0)                  {                     Console.WriteLine("\n{0} implements the interface {1}.",myType, myInterfaceList[index]);                       for(int j =0;j < myInterfaces.Length;j++)                         Console.WriteLine("Interfaces supported: {0}.", myInterfaces[j].ToString());                 }                 else                     Console.WriteLine("{0} does not implement the interface {1}.", myType,myInterfaceList[index]);               }     }     public static bool MyInterfaceFilter(Type typeObj,Object criteriaObj)     {         if(typeObj.ToString() == criteriaObj.ToString())             return true;         else             return false;     } }