none
How to find class attributes with reflection. RRS feed

  • Question

  • Suppose I have an assembly from the CSharp code provider. I want to find all classes (types?) with an [IsScript] attribute using reflection. How would I go about doing that?

    • Edited by PixxlMan Saturday, August 31, 2019 1:28 PM
    Saturday, August 31, 2019 1:24 PM

Answers

  • Try something like this:

       Assembly assembly = . . .;

       var script_types = assembly.GetTypes( ).Where( t => t.IsDefined( typeof( IsScriptAttribute ) ) );

     

    Then you can use foreach or ToList.

    • Marked as answer by PixxlMan Saturday, August 31, 2019 4:57 PM
    Saturday, August 31, 2019 2:59 PM

All replies

  • Try something like this:

       Assembly assembly = . . .;

       var script_types = assembly.GetTypes( ).Where( t => t.IsDefined( typeof( IsScriptAttribute ) ) );

     

    Then you can use foreach or ToList.

    • Marked as answer by PixxlMan Saturday, August 31, 2019 4:57 PM
    Saturday, August 31, 2019 2:59 PM
  • Awesome! That works excellently! Thank you
    • Marked as answer by PixxlMan Saturday, August 31, 2019 4:57 PM
    • Unmarked as answer by PixxlMan Saturday, August 31, 2019 4:57 PM
    Saturday, August 31, 2019 4:57 PM