How can I use reflection to find the properties which implement a specific interface?

Viewed 10637

Consider this example:

public interface IAnimal
{
}

public class Cat: IAnimal
{
}

public class DoStuff
{
    private Object catList = new List<Cat>();

    public void Go()
    {
        // I want to do this, but using reflection instead:
        if (catList is IEnumerable<IAnimal>)
            MessageBox.Show("animal list found");

        // now to try and do the above using reflection...
        PropertyInfo[] properties = this.GetType().GetProperties();
        foreach (PropertyInfo property in properties)
        {
            //... what do I do here?
            // if (*something*)
                MessageBox.Show("animal list found");
        }
    }
}

Can you complete the if statement, replacing something with the correct code?

EDIT:

I noticed that I should have used a property instead of a field for this to work, so it should be:

    public Object catList
    {
        get
        {
          return new List<Cat>();
        }
    }
3 Answers
Related