Identifying all types with some attribute
        Posted  
        
            by Tom W
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Tom W
        
        
        
        Published on 2010-04-10T10:15:39Z
        Indexed on 
            2010/04/10
            10:23 UTC
        
        
        Read the original article
        Hit count: 276
        
Hello SO;
I have an issue with .Net reflection. The concept is fairly new to me and I'm exploring it with some test cases to see what works and what doesn't. I'm constructing an example wherein I populate a set of menus dynamically by scanning through my Types' attributes.
Basically, I want to find every type in my main namespace that declares 'SomeAttribute' (doesn't matter what it is, it doesn't have any members at present). What I've done is:
    For Each itemtype As Type In Reflection.Assembly.GetExecutingAssembly().GetTypes
        If itemtype.IsDefined(Type.GetType("SomeAttribute"), False) Then
            'do something with the type
        End If
    Next
This crashes the application on startup - the first type it identifies is MyApplication which is fairly obviously not what I want. Is there a right and proper way to look for all the 'real' 'sensible' types - i.e. classes that I've defined - within the current assembly?
© Stack Overflow or respective owner