Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding types from being listed in Assembly.GetTypes in .net

Ive been looking everywhere for a possible solution to this but can't seem to find an answer. My issue is that I have a few classes that need to completely hidden from Assembly.getTypes, as I'm writing a plugin for an application, and it's picking up types that I need to remain hidden (this happens even if they are declared as private or internal classes).

anyone know how to either alter what assembly.GetTyes returns, or an ,aficionado attribute that will keep those types from being listed?

like image 622
Jeremy Tang Avatar asked Dec 19 '11 23:12

Jeremy Tang


People also ask

How do you get type objects from assemblies that have not been loaded?

GetTypes to obtain Type objects from assemblies that have not been loaded, passing in the name of the type or types you want. Use Type. GetType to get the Type objects from an assembly that is already loaded.

How to get type of assembly in c#?

GetType(String, Boolean, Boolean) Gets the Type object with the specified name in the assembly instance, with the options of ignoring the case, and of throwing an exception if the type is not found.

Which of the given methods is used in C# to get the details about information types in an assembly during runtime?

Reflection objects are used for obtaining type information at runtime. The classes that give access to the metadata of a running program are in the System. Reflection namespace.


2 Answers

This is not possible.
Sorry.

Code that calls Assembly.GetTypes() should typically filter for only public types.

like image 33
SLaks Avatar answered Oct 25 '22 14:10

SLaks


This is quite a hack and is very fragile, but could work.

Create 2 assemblies -- one for the plug-in and the second for the other types. The second would be placed in another known directory and loaded dynamically into the first when needed. (For example, via Assembly.LoadFrom.)

The first assembly would then be placed in the plug-in directory and only ever publish its types. This very fragile because you would likely have to hard-code a path to the second assembly and you run the risk of the file getting deleted or moved.

EDIT
@SLaks' comment takes away the fragility of this solution. If you embed the second assembly as a resource and load it at run-time, the app calling Assembly.GetTypes won't see the types you want hidden.

like image 107
Austin Salonen Avatar answered Oct 25 '22 14:10

Austin Salonen