Type Filtering and Method Invocation with Reflection and LINQ – Part 1

I’m currently studying for Microsoft 70-483 Programming in C# exam, and a strategy I’m trying to adopt when researching for a topic is to combine aspects of other topics that I’m unfamiliar, then writing about them.

This post combines aspects of reflection and LINQ that I’m currently looking at.

  • Assembly and Type Discovery
  • Late-Binding and Invocation
  • LINQ SelectMany and nested selects

I was wanting to write a little ‘attribute discovery’ library that could invoke methods that had been decorated with an attribute [MyMethodAttribute]. The methods themselves would be declared within a class that would also be decorated with an attribute, a different one named [MyClassAttribute].

So, when I include my “Attribute Discovery” library in an executing module, the code should  search though all the assemblies referenced by the module for classes decorated with the attribute [MyClassAttribute]. For any that are found, the code should then search these types for methods decorated with  [MyMethodAttribute].

This might sound familiar.  In fact, we encounter this pattern of attribute usage within unit test projects. Consider a simple test class:

[TestClass]
public class MyTestClass
{
    [TestMethod]
    public void MyTestMethod()
    {
    }
}

How does the framework run this? Well, without delving in and disassembling the code manually, I can’t say for sure, but I can speculate that some sort of process similar to that outlined above is used.

So, how might we achieve this ourselves? Well, that’s what we’re going to delve into in this series of articles, all under the tag EduAttrDiscover.

Up next, we setup our reference project…

One thought on “Type Filtering and Method Invocation with Reflection and LINQ – Part 1

  1. Pingback: Type Filtering and Method Invocation with Reflection and LINQ – Part 2 | James Wiseman

Leave a Reply