How can I treat Interops with an Assemblyresolve for a Class Library (DLL) project

Asked

Viewed 211 times

1

This example is from a Windows Forms project that works correctly.

namespace PriDebug_v9
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
            Application.Run(new Debug());
        }

        public static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            string assemblyFullName;

            System.Reflection.AssemblyName assemblyName;

            const string PRIMAVERA_COMMON_FILES_FOLDER = "PRIMAVERA\\SG900";

            assemblyName = new System.Reflection.AssemblyName(args.Name);

            assemblyFullName = System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.CommonProgramFilesX86), PRIMAVERA_COMMON_FILES_FOLDER, assemblyName.Name + ".DLL");

            if (System.IO.File.Exists(assemblyFullName))
            {
                return System.Reflection.Assembly.LoadFile(assemblyFullName);
            }
            else
            {
                return null;
            }

        }
    }
}

2 answers

2


The process is exactly the same, but as in a Class Library has no input method (Main), will have to put the Assembly Resolve in the class constructor (complete example):

using System;
using System.IO;
using System.Reflection;

namespace MyClassLibrary
{
    using static Environment.SpecialFolder;

    public class MyClass
    {
        const string PRIMAVERA_COMMON_FILES_FOLDER = @"PRIMAVERA\SG900";

        public MyClass() => AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;

        private Assembly AssemblyResolve(object sender, ResolveEventArgs args)
        {
            string assemblyFullName;
            AssemblyName assemblyName = new AssemblyName(args.Name);

            assemblyFullName = Path.Combine(Environment.GetFolderPath(CommonProgramFilesX86), PRIMAVERA_COMMON_FILES_FOLDER, assemblyName.Name + ".DLL");

            if (File.Exists(assemblyFullName))
                return Assembly.LoadFile(assemblyFullName);
            else
                return null;
        }
    }
}

1

Caro José

To João’s answer that is correct , I just add that in the reality of integration with the ERP V10 there is an article on the Network Developers that explains precisely with all the details (C# and VB.net) how to deal with this issue.

See the article here

  • 1

    Thank you Sérgio, this particular case is for a version 9 project. I have tried it and it does not give any error. I was trying to "force" the error to be 100% sure it was all right.

  • Hello @Josémiguelferreira. Mark replies as useful and/or correct. See Why it is important to vote.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.