Assembly Resolver


        private void ThisAddIn_Startup(object sender, System.EventArgs e) 
        {
            try
            {

                AppDomain.CurrentDomain.AssemblyResolve += eraAssemblyResolveEventHandler; } }


        /// <summary>
        /// Loads the other .dll from a specific Program Files path
        /// </summary>
        /// <remarks>This code runs when there the dll isn't in the Application folder, i.e. when deployed to users</remarks>
        private static System.Reflection.Assembly eraAssemblyResolveEventHandler(object sender, ResolveEventArgs args)
        {
            string asmPath = string.Empty;
            try
            {
                if (args.Name == null || !args.Name.StartsWith("myaddinname"))
                    return null;

                //Parse Assembly name from args.Name which uses the format: (AssemblyName, Version=, etc.)
                string asmName = args.Name.Substring(0, args.Name.IndexOf(","));

                if (asmName.Equals("Add-in-Name", StringComparison.OrdinalIgnoreCase))
                {
                    //Load the assembly and return
                    asmPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) +
                        "\\Location\\MyFileName.dll";
                    return System.Reflection.Assembly.LoadFile(asmPath);
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                //Logging and/or User Message
                string text = e.Message + Environment.NewLine + asmPath + Environment.NewLine + args.Name;
                System.Windows.Forms.MessageBox.Show(text, System.Reflection.MethodBase.GetCurrentMethod().Name, System.Windows.Forms.MessageBoxButtons.OK);
                return null;
            }
        }


© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopPrevNext