Option Strict Off vs On for Late‑Bound COM Members in VB.NET
0 reputation · 25 Dec 2023, 07:22 UTC
VB.NET Late Binding and Option Strict Compiler Behavior
When working with COM interop in VB.NET, developers must choose between Option Strict Off for runtime late binding and Option Strict On for compile‑time safety. Off allows late‑bound member access but risks runtime failures; On enforces type checks but can break existing code.
This creates a concrete constraint: how to maintain compatibility with existing COM libraries while ensuring compile‑time safety. The behavior differs significantly between VB.NET Framework and .NET Core/.NET 5+ regarding late binding resolution order, making migration between compiler versions unpredictable.
Two documented approaches exist:
- Keep Option Strict Off – Allows full late binding functionality through the My namespace and COM interop but introduces runtime performance overhead and potential runtime failures.
- Enable Option Strict On – Forces compile‑time binding checking but may break existing code that relies on late‑bound member access.
Which approach provides better long‑term maintainability when targeting both .NET Framework and .NET 6+ deployments with Office COM interop?