问题:System.TypeInitializationException:““OSGeo.GDAL.GdalPINVOKE”的类型初始值设定项引发异常。”
下面有两种安装配置的过程,均产生上述问题,介绍配置过程及解决办法:
产生过程1:用Nuget下载安装GDAL和GDAL.Native后,直接放上示例代码运行,没配置任何环境变量,就产生上述问题。
步骤1:打开一个控制台应用(.NET Framework),项目——管理Nuget程序包——浏览,如下图
步骤2:搜索GDAL与GDAL.Native,安装。
步骤3:在Debug的配置管理器——新建——x64平台,运行代码,产生上述问题。
解决:安装完GDAL和GADL.Native后,在项目文件下会有一个GdalConfiguration.cs文件
打开,将配置文件代码替换成以下代码,
using System;
using System.IO;
using System.Reflection;
using Gdal = OSGeo.GDAL.Gdal;
using Ogr = OSGeo.OGR.Ogr;
namespace ConsoleApp7
{
public static partial class GdalConfiguration
{
private static bool _configuredOgr;
private static bool _configuredGdal;
/// <summary>
/// Function to determine which platform we're on
/// </summary>
private static string GetPlatform()
{
return IntPtr.Size == 4 ? "x86" : "x64";
}
/// <summary>
/// Construction of Gdal/Ogr
//