Search code examples
reflectionpostsharp

How to find a class tagged with postsharp aspect via reflection


I tryout some ways to use postsharp

The definition of one of my custom aspects looks like:

<Serializable> _
<MulticastAttributeUsage(PersistMetaData:=True)> _
Public Class StringChecker
    Inherits LocationInterceptionAspect

    Public Overrides Sub OnGetValue(ByVal args As LocationInterceptionArgs)
        MyBase.OnGetValue(args)
        ..... ..... .....

When I try to find all classes that are tagged with the attribute / aspect , the result ist always empty. I use this code to discover the assemblys at runtime:

Dim targetClasses As IEnumerable(Of Type) = From asm In AppDomain.CurrentDomain.GetAssemblies().Where(Function(A) A.FullName.Contains("MyNameSpace")) _
                                                .SelectMany(Function(A) A.GetTypes()) _
                                                .Where(Function(T) T.IsDefined(GetType(StringChecker), True))

When I use for testing proposals, a normal atttribute, the query delivers the espected types. Hoppefully some can give me a hint what I have to do.

edit: It looks like the custom attributes can only be found in the class propertys..

Regards, Markus


Solution

  • There are two ways to do it:

    1. Use ReflectionSearch but it does not retrieve aspects added with IAspectProvider.
    2. Use IAspectRepositoryService