Get all derived types of a type

2019-01-02 18:22发布

Is there a better (more performant or nicer code ;) way to find all derived Types of a Type? Currently im using something like:

  1. get all types in used Assemblies
  2. check my type with all those types if it is 'IsAssignable'

I was wondering if theres a better way todo this?

标签: c# .net
6条回答
明月照影归
2楼-- · 2019-01-02 18:44

I once used this Linq-method to get all types inheriting from a base type B:

    var listOfBs = (from domainAssembly in AppDomain.CurrentDomain.GetAssemblies()
                    from assemblyType in domainAssembly.GetTypes()
                    where typeof(B).IsAssignableFrom(assemblyType)
                    select assemblyType).ToArray();

EDIT: As this still seems to get more rep (thus more views), let me add some more details:

  • As the above-mentioned link states, this method uses Reflection on each call. So when using the method repeatedly for the same type, one could probably make it much more efficient by loading it once.
  • As Anton suggests, maybe you could (micro)optimize it using domainAssembly.GetExportedTypes() to retrieve only publicly visible types (if that's all you need).
  • As Noldorin mentions, Type.IsAssignable will also get the original (non-derived) type. (Type.IsSubclassOf will not, but Type.IsSubclassOf will not work if the base type is an interface).
  • One may want/need to check for a 'real' class: && ! assemblyType.IsAbstract. (Note that all interfaces are considered abstract, see MSDN.)
查看更多
素衣白纱
3楼-- · 2019-01-02 18:45

I'm pretty sure the method you suggested is going to be the easier way to find all derived types. Parent classes don't store any information about what their sub-classes are (it would be quite silly if they did), which means there's no avoiding a search through all the types here.

Only recommendation is to use the Type.IsSubclassOf method instead of Type.IsAssignable in order to check whether a particular type is derived from another. Still, perhaps there is a reason you need to use Type.IsAssignable (it works with interfaces, for example).

查看更多
还给你的自由
4楼-- · 2019-01-02 18:52

I think there is no better or direct way.

Better: Use IsSubclassOf instead of IsAssignable.

查看更多
高级女魔头
5楼-- · 2019-01-02 18:53

If you're just interested in browsing, then .NET Reflector has the ability to do this. However, it isn't something that's really feasible. Would you want all types that are in the currently loaded assemblies? Assemblies referenced by the executing assembly? There are many different ways to obtain a list of Types, and writing something that would account for (and provide options for) would be a pretty big cost with relatively low benefit.

What are you trying to do? There's likely a better (or at least more efficient) way to accomplish it.

查看更多
听够珍惜
6楼-- · 2019-01-02 18:55

Asuming baseType contains a System.Type object that you want to check against and matchType contains a System.Type object with the type of your current iteration (through a foreach-loop or whatever):

If you want to check wheather matchType is derived from the class represented by baseType I'd use

matchType.IsSubclassOf(baseType)

And if you want to check wheather matchType implements the interface represented by baseType I'd use

matchType.GetInterface(baseType.ToString(), false) != null

Of course I'd store baseType.ToString() as a global variable so I wouldn't need to call it all the time. And since you probably would need this in a context where you have a lot of types, you could also consider using the System.Threading.Tasks.Parallel.ForEach-Loop to iterate through all your types...

查看更多
弹指情弦暗扣
7楼-- · 2019-01-02 19:08

The only optimization you can squeeze out of this is to use Assembly.GetExportedTypes() to retrieve only publicly visible types if that's the case. Other than that, there's no way to speed things up. LINQ may help with readability side of things, but not performance-wise.

You can do some short-circuiting to avoid unnecessary calls to IsAssignableFrom which is, according to Reflector, quite expensive one, by first testing whether the type in question is of required "class". That is, you're searching for classes only, there's no point in testing enums or arrays for "assignability".

查看更多
登录 后发表回答