ArcGIS 10.0⇒10.1


ライセンス初期化用のEnumが変わっており鬱陶しいことこの上ない。
とりあえず共通化しておくことに

これでビルド程度で済むか。
たまにコントロール貼り換えしないと不幸になることがあるので、そこだけ注意

共通化、手法は以下

構成は以下
共通DLL
100用DLL
101用DLL
PGでは共通DLLのみ読み込み、初期化処理自体は、各バージョンの
LicenseInitializer.csに一任

基底クラスを作っておく、これを100版と101版で作る

    public abstract class LicenseInit : IDisposable
    {

        public abstract string Version { get; }

        public abstract bool InitializeApplication(Products[] productCodes, Extensions[] extensions);

        public abstract void ShutdownApplication();

        public void Dispose()
        {
            this.ShutdownApplication();
        }//end method
    }//end class

同DLLでFactoryとプロダクト定義を作っておく

    public enum Products
    {
        Engine = 10,
        GeoDBExt = 20,
        Server = 30,
        ArcViewBasic = 40,
        ArcEditorStd = 50,
        ArcInfoAdv = 60
    }//end enum

    public enum Extensions
    {
        Network = 8,
        t3DAnalyst = 9,
        SpatialAnalyst = 10
    }//end enum

    public class LicenseInitFactory
    {
        public static LicenseInit Create()
        {
            string version = null;
            foreach (var item in ESRI.ArcGIS.RuntimeManager.InstalledRuntimes)
            {
                version = item.Version;
                System.Diagnostics.Debug.Print("{0}:{1}", item.Product.ToString("G"), item.Version);
                break;
            }//end loop

            string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            dir = Path.Combine(dir, "LicenseInit");

            DirectoryInfo dirInfo = new DirectoryInfo(dir);
            if (!dirInfo.Exists)
                return null;
            foreach (var file in dirInfo.GetFiles())
            {
		        if (file.Extension.ToLower() != ".dll")
                    continue;
                try
                {
                    Assembly asm = Assembly.LoadFile(file.FullName);
                    LicenseInit init = getFromAsm(asm, version);

                    if (init == null)
                        continue;

                    return init;
                }
                catch (Exception ex)
                {
                    Debug.Print(ex.Message);
                    Debug.Print(ex.StackTrace);

                    continue;
                }
            }//end file loop

            return null;
        }//end method

        private static LicenseInit getFromAsm(Assembly asm ,string version)
        {
            Type[] types = asm.GetTypes();

            foreach (var type in types)
            {
                if (type.IsClass &&
                    type.IsPublic &&
                    type.BaseType == typeof(LicenseInit) &&
                    !type.IsAbstract)
                {
                    try
                    {
                        LicenseInit lInit = asm.CreateInstance(type.FullName, true) as
                        LicenseInit;

                        if (lInit.Version == version)
                            return lInit;
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                        Debug.WriteLine(ex.StackTrace);
                        continue;
                    }//end try
                }//end if
            }//end loop
            return null;

        }//end method
    }//end class

===== 以下,101の例 =====

    /******************************************************************/
    public class LicenseInit101 : LicenseInit
    {
        /******************************************************************/
        private static LicenseInitializer m_AOLicenseInitializer = null;

        /******************************************************************/
        public LicenseInit101()
        {
            LicenseInit101.m_AOLicenseInitializer = new LicenseInitializer();
        }

        /******************************************************************/
        public override string Version
        {
            get { return "10.1"; }
        }

        /******************************************************************/
        public override bool InitializeApplication(Products[] productCodes, Extensions[] extensions)
        {
            esriLicenseProductCode[] esriProducts = this.getCode(productCodes);
            esriLicenseExtensionCode[] esriExt = this.getExt(extensions);

            return m_AOLicenseInitializer.InitializeApplication(esriProducts, esriExt);
        }//end method

        /******************************************************************/
        public override void ShutdownApplication()
        {
            m_AOLicenseInitializer.ShutdownApplication();
        }//end method

        /******************************************************************/
        private esriLicenseProductCode[] getCode(Products[] products)
        {
            if (products == null || products.Length == 0)
                return new esriLicenseProductCode[0];

            List<esriLicenseProductCode> results = new List<esriLicenseProductCode>(products.Length);

            foreach (var item in products)
            {
                esriLicenseProductCode code = (esriLicenseProductCode)Enum.Parse(typeof(esriLicenseProductCode), ((int)item).ToString());
                results.Add(code);
            }
            return results.ToArray();
        }//end method

        /******************************************************************/
        private esriLicenseExtensionCode[] getExt(Extensions[] exts)
        {
            if (exts == null || exts.Length == 0)
                return new esriLicenseExtensionCode[0];

            List<esriLicenseExtensionCode> results = new List<esriLicenseExtensionCode>(exts.Length);

            foreach (var item in exts)
            {
                esriLicenseExtensionCode code = (esriLicenseExtensionCode)Enum.Parse(typeof(esriLicenseProductCode), ((int)item).ToString());
                results.Add(code);
            }
            return results.ToArray();
        }//end method
    }//end class
カテゴリー: 開発 タグ: パーマリンク