I have a non-public type in my .NET assembly, which I'm loading with reflection from inside this assembly.
This works great in debug builds, but in release builds the linker removes this type because:
- it is internal
- it is not referenced in the assembly's code
I could switch off this behavior for the whole project or make the type public, but that's not what I want. Instead, I'm looking for a way to tell the linker to add the type in any case. Ideally something like the exact opposite of the System::Runtime::CompilerServices::DiscardableAttribute.
The easiest way to do this would be to reference the type. (I presume there's a reason to use reflection to find the type, rather than referencing it directly.) You could do this in the method where you do the reflection, or in the static constructor of any public type. Wherever you do this, as long as you document what's going on and why you're doing it, it should be fine.
Type^ keepThis = InternalOnlyClass::typeid;
in a method is probably all you need. If the compiler optimizes that statement out, theninternal: static Type^ keepThis = InternalOnlyClass::typeid;
should do the trick.