Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assembly.GetTypes - why use this if GetExportedTypes is available?

I'm confused about what scenarios you would use one or the other.

If you have an assembly with a some public and private (or internal) types in it, then only the public types should be available from outside. Any types that are internal, or private - should not be available, in fact, their existence should not be discoverable.

Therefore, GetTypes and GetExportedTypes - in my mind, should return the same thing.

Clearly I'm thinking about this wrong - what is each one for?

Thanks!

like image 610
Coopernick Avatar asked Feb 03 '23 12:02

Coopernick


1 Answers

From the MSDN docs:

Assembly.GetTypes Method
Return Value Type: System.Type[]
An array that contains all the types that are defined in this assembly.

From the MSDN docs:

Assembly.GetExportedTypes Method
Return Value
Type: System.Type[]
An array that represents the types defined in this assembly that are visible outside the assembly.

So the GetTypes() call will indeed give you all types defined in an assembly - whether they're "visible" and instantiable to you or not. Might seem odd - but what if you want to inspect yourself, your own assembly (or an assembly in the same namespace as your code)? You need to be able to see everything - if needed.

like image 88
marc_s Avatar answered Feb 06 '23 11:02

marc_s