Thursday, October 11, 2007

LINQ to Reflection 2

In the first reflection example, we just pulled out the types from the loaded assemblies that matched our criteria. In this example, we're searching through an array of objects and returning those that support a specific interface.

object[] objectArray = {new Queue<object>(), new StreamWriter(@"C:\temp.txt"), new List<object>()};

var supportsIEnumerable = (from obj in objectArray
from supportedInterface in obj.GetType().GetInterfaces()
where supportedInterface == typeof(IEnumerable)
select obj).ToList();

There are 2 objects in the supportsIEnumerable list.

No comments: