Reflection: How to create a precisely filtered list of the Assembly's types?

Lorry Laurence mcLarry
foreach (var item in Assembly.GetExecutingAssembly().GetTypes()
                             .Where(x => x.BaseType == typeof(Item)))
{
    comboBox1.Items.Add(item);
}
comboBox1.DisplayMember = "Name";

Can this be modified so that the list will include all classes that are derived from 'Item' even if they are two or more times removed? (ie: x.BaseType.BaseType == typeof(Item) etc.)

Also, could it be done to filter out all abstract classes?

vcsjones

A simple approach to this is to use IsAssignableFrom.

foreach (var item in Assembly.GetExecutingAssembly().GetTypes()
                             .Where(x => typeof(Item).IsAssignableFrom(x) && !x.IsAbstract))
{
    //Handle each item
}

Assuming my "Handle each item were a Console.WriteLine(item), and I had this class structure:

class ItemBase {}
class Item : ItemBase {}
class SuperItem : Item {}
abstract class SuperSuperItem : SuperItem{}
class UltimateItem : SuperSuperItem {}

The output would be:

Item

SuperItem

UltimateItem

Note that in this case IsAssignableFrom returns true for itself, which you can easily omit by adding && x != typeof(Item) to the Where clause.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Reflection - how to compare between list of types

From Dev

How to access a referenced assembly's types?

From Dev

how to create a list of type obtained from reflection

From Dev

How to decode ProGuard's obfuscated code precisely?

From Dev

Reflection - create and register constructors dynamically from assembly

From Dev

Load Assembly and create class using Reflection

From Dev

How to create a new List<T> knowing the Type through reflection?

From Dev

How to create a filtered view of a directory?

From Dev

c# - Different types when loading from assembly using reflection

From Dev

How to reflect changes in a filtered list

From Dev

Getting list properties types using reflection

From Dev

How to filter precisely on date?

From Dev

How can I compare value-types acquired from Reflection's "GetValue"?

From Dev

How create button using reflection?

From Dev

How to use reflection to handle all types?

From Dev

How to create filtered DataFrame with minimum code

From Dev

How to create barplot from filtered data in Shiny?

From Dev

How to create barplot from filtered data in Shiny?

From Dev

how to create filtered image using matlab?

From Dev

Create instance of List using reflection c#

From Dev

How to create a linked list sorted by multiple data types

From Dev

Generic reflection, how to get a list?

From Dev

Generic reflection, how to get a list?

From Dev

How to use nth-child on a filtered list?

From Dev

How to create new instance of a class by passing Object[] instead of parameter list with reflection

From Dev

Extension method for precisely two different types

From Dev

Get datagrid to create columns with unknown types using reflection

From Dev

How can I list supported filesystems? More precisely, how can I determine if filesystems such as NTFS are supported?

From Dev

How to load an assembly as reflection-only in a new AppDomain?

Related Related

  1. 1

    Reflection - how to compare between list of types

  2. 2

    How to access a referenced assembly's types?

  3. 3

    how to create a list of type obtained from reflection

  4. 4

    How to decode ProGuard's obfuscated code precisely?

  5. 5

    Reflection - create and register constructors dynamically from assembly

  6. 6

    Load Assembly and create class using Reflection

  7. 7

    How to create a new List<T> knowing the Type through reflection?

  8. 8

    How to create a filtered view of a directory?

  9. 9

    c# - Different types when loading from assembly using reflection

  10. 10

    How to reflect changes in a filtered list

  11. 11

    Getting list properties types using reflection

  12. 12

    How to filter precisely on date?

  13. 13

    How can I compare value-types acquired from Reflection's "GetValue"?

  14. 14

    How create button using reflection?

  15. 15

    How to use reflection to handle all types?

  16. 16

    How to create filtered DataFrame with minimum code

  17. 17

    How to create barplot from filtered data in Shiny?

  18. 18

    How to create barplot from filtered data in Shiny?

  19. 19

    how to create filtered image using matlab?

  20. 20

    Create instance of List using reflection c#

  21. 21

    How to create a linked list sorted by multiple data types

  22. 22

    Generic reflection, how to get a list?

  23. 23

    Generic reflection, how to get a list?

  24. 24

    How to use nth-child on a filtered list?

  25. 25

    How to create new instance of a class by passing Object[] instead of parameter list with reflection

  26. 26

    Extension method for precisely two different types

  27. 27

    Get datagrid to create columns with unknown types using reflection

  28. 28

    How can I list supported filesystems? More precisely, how can I determine if filesystems such as NTFS are supported?

  29. 29

    How to load an assembly as reflection-only in a new AppDomain?

HotTag

Archive