Searching for methods with custom attributes in nested class structure

Does anyone know of a good way to do this? Say I have a class called Garage and in that class there is a property of type List<Cars> that contains objects of a Car class. Some methods in the Garage class and in the Car class have a custom attribute. I'd like to find those methods and invoke them.

It would be trivial to do this if I know the structure of the Garage class, but what if I don't?

In other words, is there a good way to create a method where any object can be passed to it, the method traverses the entire object, including any enumerables within that object, and any enumerables within those objects, etc., to find and invoke methods with a certain attribute?

Right now I'm using reflection and a recursive method for this, but the PropertyInfo.GetValue method is really picky when you start having to deal with lists and enumerables. Has anyone solved a problem like this before?

by nickcut via /r/csharp

Leave a Reply