When, if ever, is it safe to wrap behind a conditional code that requires an assembly that may not be present?
For example:
if (SafeCheckForOptionalAssembly()) {
// Code requiring an optional assembly
} else {
// fallback: don't rely on optional assembly
}
Directly related questions:
- Does it matter if the conditionally included code uses a type contained within the optional assembly? (e.g.
var foo = new MyClassInOptionalAssembly()) - Does the behavior change if the conditionally included code is wrapped in a method?
Yes, doesn’t work. The jitter will bomb trying to compile the method before it starts running. The exception is raised in the calling method.
Possibly. But to be 100% sure, you’ll need to apply the [MethodImpl(MethodImplOptions.NoInlining)] attribute so the method never gets inlined.
These assumptions are only valid for a jitter that compiles on demand. The Microsoft x86 and x64 jitters do. The Mono jitter doesn’t (last I looked).