private SafeHandle GetRegistryHandle(RegistryKey key) { var hkey = key.GetType().GetField("hkey", BindingFlags.Instance | BindingFlags.NonPublic); if (null != hkey) { return (SafeHandle)hkey.GetValue(key); } else { throw new InvalidOperationException("Field 'hkey' was not found in the RegistryKey class."); } }