格式太舊或是類型庫無效。 (Exception from HRESULT: 0x80028019 (TYPE_E_UNSUPFORMAT))


錯誤提示信息

格式太舊或是類型庫無效。 (Exception from HRESULT: 0x80028019 (TYPE_E_UNSUPFORMAT)) 。

Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))。

出現時間

並行大批量數據處理操作時隨機出現(使用的是ArcGIS COM庫)。

原因:

這是網絡中找到的使用Excel進行COM對象互操作時的解釋:

After digging the internet I found out that there is a bug in Microsoft Interop with COM objects (at least with my case which is MS Excel 2010).

The bug is that .NET checks that your thread (C# or VB code) localization is suitable to MS Excel localization you installed earlier, and if not it tells that the Microsoft.Office.Interop library is old or invalid.

Your thread localization is derived from your computer regional settings (from the control panel --> regional and language)

Then there are two options to solve this problem:

  1. To change your thread localization (by code)
  2. Install language pack for your Office

解決方案:

線程啟動時,設置當前線程的CultureInfo與互操作庫一致。

When a thread is started, its culture is initially determined by using GetUserDefaultLCID from the Windows API. I found no way (I assume there is no way) to override this behavior. Only thing you can do is to set the thread culture afterwards.

I wrote an extension. For that:

public static class ParallelQueryCultureExtensions
{
    public static ParallelQuery<TSource> SetCulture<TSource>(this ParallelQuery<TSource> source, CultureInfo cultureInfo)
    {
        SetCulture(cultureInfo);
        return source
            .Select(
                item =>
                    {
                        SetCulture(cultureInfo);
                        return item;
                    });
    }

    private static void SetCulture(CultureInfo cultureInfo) {
        if (Thread.CurrentThread.CurrentCulture != cultureInfo) {
            Thread.CurrentThread.CurrentCulture = cultureInfo;
        }
    }
} 

So if you use it just after splitting up the original source using .AsParallel() you will get what you want.

注:

當內存溢出時,也可能導致該錯誤的出現。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM