Beispiel #1
0
            private static bool CreateLockFree(bool suppressCOMExceptions = true, bool suppressOtherExceptions = true)
            {
                if (Singleton.ComFactory != null)
                {
                    try
                    {
                        Marshal.ReleaseComObject(Singleton.ComFactory);
                    }
                    catch
                    {
                        // do nothing
                    }

                    Singleton.ComFactory = null;
                }

                bool success = false;
                ISpellCheckerFactory result = null;

                try
                {
                    result  = new SpellCheckerFactoryClass();
                    success = true;
                }
                catch (COMException) when(suppressCOMExceptions)
                {
                    // do nothing
                }
                catch (UnauthorizedAccessException uae)
                {
                    // Sometimes, COM initialization can throw UnauthorizedAccessException
                    // We will treat it the same as COMException
                    if (!suppressCOMExceptions)
                    {
                        throw new COMException(string.Empty, uae);
                    }
                }
                catch (InvalidCastException icex)
                {
                    // Sometimes, InvalidCastException is thrown when SpellCheckerFactory fails to instantiate correctly
                    // We will treat it as if it were a COM Exception and translate it to one.
                    if (!suppressCOMExceptions)
                    {
                        throw new COMException(string.Empty, icex);
                    }
                }
                catch (Exception e) when(suppressOtherExceptions && !(e is COMException) && !(e is UnauthorizedAccessException))
                {
                    // do nothing
                }

                if (success)
                {
                    Singleton.ComFactory = result;
                }

                return(success);
            }
 public void CanCreateSpellChecker_Regression_core_1994()
 {
     // regression test for https://github.com/dotnet/core/issues/1994
     // ensure that we can create this object which implements IClassFactory2
     // we're mainly concerned that we don't AV
     try
     {
         var spellCheck = new SpellCheckerFactoryClass();
     }
     catch (InvalidCastException)
     { }
 }