vendredi 27 novembre 2015

How can i return reflected object from AppDomain Assembly.LoadFrom

I need return objects, from 2 versions of dlls in one process. I'm doing this incompatible with each other, and i want to connect to 2 types of MS WHQL Servers in one process. I'm trying use this:

namespace AppDomainTester
{
    public class Program
    {
        [STAThread]
        public static void Main(string[] args)
        {
            string pathToDll = Assembly.GetExecutingAssembly().Location;
            string HCKpath = Path.Combine(Path.GetDirectoryName(pathToDll), @"WhqlDlls\HCK",
                @"microsoft.windows.kits.hardware.objectmodel.dbconnection.dll");
            string HLKpath = Path.Combine(Path.GetDirectoryName(pathToDll), @"WhqlDlls\HLK",
                @"microsoft.windows.kits.hardware.objectmodel.dbconnection.dll");
            try
            {
                object hck;
                object hlk;
                // HCK 
                AppDomainSetup domainSetupHck = new AppDomainSetup
                {
                    PrivateBinPath = pathToDll
                };
                AppDomain domainHck = AppDomain.CreateDomain("HCK", null, domainSetupHck);
                InstanceProxy proxyHck =
                    domainHck.CreateInstanceFromAndUnwrap(pathToDll, typeof (InstanceProxy).FullName) as InstanceProxy;
                if (proxyHck != null)
                {
                    object obj = new object();
                    hck = proxyHck.LoadLibrary(HCKpath, "tfs13-com-hck1");
                }
                //HLK
                AppDomainSetup domainSetupHlk = new AppDomainSetup
                {
                    PrivateBinPath = pathToDll
                };
                AppDomain domainHlk = AppDomain.CreateDomain("HLK", null, domainSetupHlk);
                InstanceProxy proxyHlk =
                    domainHck.CreateInstanceFromAndUnwrap(pathToDll, typeof (InstanceProxy).FullName) as InstanceProxy;

                if (proxyHlk != null)
                {
                    object obj = new object();
                    hlk = proxyHlk.LoadLibrary(HCKpath, "tfs13-com-hck1");
                }
                //var some = domainHck.GetAssemblies();

                AppDomain.Unload(domainHck);
                AppDomain.Unload(domainHlk);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace);
            }

        }
    }

    internal class InstanceProxy : MarshalByRefObject
    {

        private object obj;

        public object LoadLibrary(string path, string controller)
        {
            Assembly asm = Assembly.LoadFrom(path);
            Type[] types = asm.GetExportedTypes();
            Type type =
                types.FirstOrDefault(
                    t =>
                        (t.FullName == "Microsoft.Windows.Kits.Hardware.ObjectModel.DBConnection.DatabaseProjectManager"));
            if (type != null)
            {
                var constructor = type.GetConstructors().Single(x => x.GetParameters().Length == 1);
                obj = constructor.Invoke(new[] {controller});
            }
            return null;
        }
    }
}

I want to return obj from LoadLibrary method, but if i try return obj;, i recieve an exception :( What am I doing wrong?





Aucun commentaire:

Enregistrer un commentaire