static Exception GetExceptionForHRInternal(int errorCode, IntPtr errorInfo) { switch (errorCode) { case HResults.S_OK: case HResults.S_FALSE: return(null); case HResults.COR_E_AMBIGUOUSMATCH: return(new System.Reflection.AmbiguousMatchException()); case HResults.COR_E_APPLICATION: return(new System.ApplicationException()); case HResults.COR_E_ARGUMENT: return(new System.ArgumentException()); case HResults.COR_E_ARGUMENTOUTOFRANGE: return(new System.ArgumentOutOfRangeException()); case HResults.COR_E_ARITHMETIC: return(new System.ArithmeticException()); case HResults.COR_E_ARRAYTYPEMISMATCH: return(new System.ArrayTypeMismatchException()); case HResults.COR_E_BADEXEFORMAT: return(new System.BadImageFormatException()); case HResults.COR_E_BADIMAGEFORMAT: return(new System.BadImageFormatException()); //case HResults.COR_E_CODECONTRACTFAILED: //return new System.Diagnostics.Contracts.ContractException (); //case HResults.COR_E_COMEMULATE: case HResults.COR_E_CUSTOMATTRIBUTEFORMAT: return(new System.Reflection.CustomAttributeFormatException()); case HResults.COR_E_DATAMISALIGNED: return(new System.DataMisalignedException()); case HResults.COR_E_DIRECTORYNOTFOUND: return(new System.IO.DirectoryNotFoundException()); case HResults.COR_E_DIVIDEBYZERO: return(new System.DivideByZeroException()); case HResults.COR_E_DLLNOTFOUND: return(new System.DllNotFoundException()); case HResults.COR_E_DUPLICATEWAITOBJECT: return(new System.DuplicateWaitObjectException()); case HResults.COR_E_ENDOFSTREAM: return(new System.IO.EndOfStreamException()); case HResults.COR_E_ENTRYPOINTNOTFOUND: return(new System.EntryPointNotFoundException()); case HResults.COR_E_EXCEPTION: return(new System.Exception()); case HResults.COR_E_EXECUTIONENGINE: return(new System.ExecutionEngineException()); case HResults.COR_E_FIELDACCESS: return(new System.FieldAccessException()); case HResults.COR_E_FILELOAD: return(new System.IO.FileLoadException()); case HResults.COR_E_FILENOTFOUND: return(new System.IO.FileNotFoundException()); case HResults.COR_E_FORMAT: return(new System.FormatException()); case HResults.COR_E_INDEXOUTOFRANGE: return(new System.IndexOutOfRangeException()); case HResults.COR_E_INSUFFICIENTEXECUTIONSTACK: return(new System.InsufficientExecutionStackException()); case HResults.COR_E_INVALIDCAST: return(new System.InvalidCastException()); case HResults.COR_E_INVALIDFILTERCRITERIA: return(new System.Reflection.InvalidFilterCriteriaException()); case HResults.COR_E_INVALIDOLEVARIANTTYPE: return(new System.Runtime.InteropServices.InvalidOleVariantTypeException()); case HResults.COR_E_INVALIDOPERATION: return(new System.InvalidOperationException()); case HResults.COR_E_INVALIDPROGRAM: return(new System.InvalidProgramException()); case HResults.COR_E_IO: return(new System.IO.IOException()); case HResults.COR_E_MARSHALDIRECTIVE: return(new System.Runtime.InteropServices.MarshalDirectiveException()); case HResults.COR_E_MEMBERACCESS: return(new System.MemberAccessException()); case HResults.COR_E_METHODACCESS: return(new System.MethodAccessException()); case HResults.COR_E_MISSINGFIELD: return(new System.MissingFieldException()); case HResults.COR_E_MISSINGMANIFESTRESOURCE: return(new System.Resources.MissingManifestResourceException()); case HResults.COR_E_MISSINGMEMBER: return(new System.MissingMemberException()); case HResults.COR_E_MISSINGMETHOD: return(new System.MissingMethodException()); case HResults.COR_E_MULTICASTNOTSUPPORTED: return(new System.MulticastNotSupportedException()); case HResults.COR_E_NOTFINITENUMBER: return(new System.NotFiniteNumberException()); case HResults.COR_E_NOTSUPPORTED: return(new System.NotSupportedException()); case HResults.COR_E_NULLREFERENCE: return(new System.NullReferenceException()); case HResults.COR_E_OBJECTDISPOSED: return(new System.ObjectDisposedException("")); case HResults.COR_E_OPERATIONCANCELED: return(new System.OperationCanceledException()); case HResults.COR_E_OUTOFMEMORY: return(new System.OutOfMemoryException()); case HResults.COR_E_OVERFLOW: return(new System.OverflowException()); case HResults.COR_E_PATHTOOLONG: return(new System.IO.PathTooLongException()); case HResults.COR_E_PLATFORMNOTSUPPORTED: return(new System.PlatformNotSupportedException()); case HResults.COR_E_RANK: return(new System.RankException()); case HResults.COR_E_REFLECTIONTYPELOAD: return(new System.MissingMethodException()); case HResults.COR_E_RUNTIMEWRAPPED: return(new System.MissingMethodException()); case HResults.COR_E_SECURITY: return(new System.Security.SecurityException()); case HResults.COR_E_SERIALIZATION: return(new System.Runtime.Serialization.SerializationException()); case HResults.COR_E_STACKOVERFLOW: return(new System.StackOverflowException()); case HResults.COR_E_SYNCHRONIZATIONLOCK: return(new System.Threading.SynchronizationLockException()); case HResults.COR_E_SYSTEM: return(new System.SystemException()); case HResults.COR_E_TARGET: return(new System.Reflection.TargetException()); case HResults.COR_E_TARGETINVOCATION: return(new System.MissingMethodException()); case HResults.COR_E_TARGETPARAMCOUNT: return(new System.Reflection.TargetParameterCountException()); case HResults.COR_E_THREADABORTED: return(new System.Threading.ThreadAbortException()); case HResults.COR_E_THREADINTERRUPTED: return(new System.Threading.ThreadInterruptedException()); case HResults.COR_E_THREADSTART: return(new System.Threading.ThreadStartException()); case HResults.COR_E_THREADSTATE: return(new System.Threading.ThreadStateException()); case HResults.COR_E_TYPEACCESS: return(new System.TypeAccessException()); case HResults.COR_E_TYPEINITIALIZATION: return(new System.TypeInitializationException("")); case HResults.COR_E_TYPELOAD: return(new System.TypeLoadException()); case HResults.COR_E_TYPEUNLOADED: return(new System.TypeUnloadedException()); case HResults.COR_E_UNAUTHORIZEDACCESS: return(new System.UnauthorizedAccessException()); //case HResults.COR_E_UNSUPPORTEDFORMAT: case HResults.COR_E_VERIFICATION: return(new System.Security.VerificationException()); //case HResults.E_INVALIDARG: case HResults.E_NOTIMPL: return(new System.NotImplementedException()); //case HResults.E_POINTER: case HResults.RO_E_CLOSED: return(new System.ObjectDisposedException("")); case HResults.COR_E_ABANDONEDMUTEX: case HResults.COR_E_AMBIGUOUSIMPLEMENTATION: case HResults.COR_E_CANNOTUNLOADAPPDOMAIN: case HResults.COR_E_CONTEXTMARSHAL: case HResults.COR_E_HOSTPROTECTION: case HResults.COR_E_INSUFFICIENTMEMORY: case HResults.COR_E_INVALIDCOMOBJECT: case HResults.COR_E_KEYNOTFOUND: case HResults.COR_E_MISSINGSATELLITEASSEMBLY: case HResults.COR_E_SAFEARRAYRANKMISMATCH: case HResults.COR_E_SAFEARRAYTYPEMISMATCH: case HResults.COR_E_SAFEHANDLEMISSINGATTRIBUTE: case HResults.COR_E_SEMAPHOREFULL: case HResults.COR_E_THREADSTOP: case HResults.COR_E_TIMEOUT: case HResults.COR_E_WAITHANDLECANNOTBEOPENED: case HResults.DISP_E_OVERFLOW: case HResults.E_BOUNDS: case HResults.E_CHANGED_STATE: case HResults.E_FAIL: case HResults.E_HANDLE: case HResults.ERROR_MRM_MAP_NOT_FOUND: case HResults.TYPE_E_TYPEMISMATCH: case HResults.CO_E_NOTINITIALIZED: case HResults.RPC_E_CHANGED_MODE: return(new COMException("", errorCode)); case HResults.STG_E_PATHNOTFOUND: case HResults.CTL_E_PATHNOTFOUND: { var ex = new System.IO.DirectoryNotFoundException(); ex.SetErrorCode(errorCode); return(ex); } case HResults.FUSION_E_INVALID_PRIVATE_ASM_LOCATION: case HResults.FUSION_E_SIGNATURE_CHECK_FAILED: case HResults.FUSION_E_LOADFROM_BLOCKED: case HResults.FUSION_E_CACHEFILE_FAILED: case HResults.FUSION_E_ASM_MODULE_MISSING: case HResults.FUSION_E_INVALID_NAME: case HResults.FUSION_E_PRIVATE_ASM_DISALLOWED: case HResults.FUSION_E_HOST_GAC_ASM_MISMATCH: case HResults.COR_E_MODULE_HASH_CHECK_FAILED: case HResults.FUSION_E_REF_DEF_MISMATCH: case HResults.SECURITY_E_INCOMPATIBLE_SHARE: case HResults.SECURITY_E_INCOMPATIBLE_EVIDENCE: case HResults.SECURITY_E_UNVERIFIABLE: case HResults.COR_E_FIXUPSINEXE: case HResults.ERROR_TOO_MANY_OPEN_FILES: case HResults.ERROR_SHARING_VIOLATION: case HResults.ERROR_LOCK_VIOLATION: case HResults.ERROR_OPEN_FAILED: case HResults.ERROR_DISK_CORRUPT: case HResults.ERROR_UNRECOGNIZED_VOLUME: case HResults.ERROR_DLL_INIT_FAILED: case HResults.FUSION_E_CODE_DOWNLOAD_DISABLED: case HResults.CORSEC_E_MISSING_STRONGNAME: case HResults.MSEE_E_ASSEMBLYLOADINPROGRESS: case HResults.ERROR_FILE_INVALID: { var ex = new System.IO.FileLoadException(); ex.SetErrorCode(errorCode); return(ex); } case HResults.CTL_E_FILENOTFOUND: { var ex = new System.IO.FileNotFoundException(); ex.SetErrorCode(errorCode); return(ex); } default: throw new NotImplementedException(errorCode.ToString()); } }