internal ResolverHelper(AddInToken pipeline) : this(pipeline._contract)
 {
 }
        internal static T Activate <T>(AddInToken token, AddInProcess process, AddInSecurityLevel level)
        {
            PermissionSet permissionSet = GetPermissionSetForLevel(level);

            return(Activate <T>(token, process, permissionSet));
        }
        private static T ActivateInAppDomain <T>(AddInToken pipeline, AppDomain domain, AddInControllerImpl controller, bool weOwn)
        {
            ContractComponent contract    = pipeline._contract;
            HostAdapter       hostAdapter = pipeline._hostAdapter;

            bool usingHostAppDomain = domain == AppDomain.CurrentDomain;

            //begin direct connect code
            if (AddInToken.EnableDirectConnect && !weOwn && usingHostAppDomain)
            {
                Type     havType     = typeof(T);
                TypeInfo havTypeInfo = new TypeInfo(havType);

                if (pipeline._addinBase.CanDirectConnectTo(havTypeInfo))
                {
                    // Connect directly for best performance.
                    // Assert permission to the specific Addin directory only.
                    PermissionSet permissionSet = new PermissionSet(PermissionState.None);
                    permissionSet.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery,
                                                                     Path.GetDirectoryName(pipeline._addin.Location)));
                    permissionSet.Assert();

                    Assembly addInAssembly = Assembly.LoadFrom(pipeline._addin.Location);
                    //
                    Type   addinType = addInAssembly.GetType(pipeline._addin.TypeInfo.FullName, true);
                    Object addIn     = addinType.GetConstructor(new Type[0]).Invoke(new Object[0]);
                    System.Diagnostics.Contracts.Contract.Assert(addIn != null, "Bypass couldn't create the add-in");

                    // remember the addin directly as the HAV.  Set the contract to null.
                    controller.AssociateWithHostAddinView(addIn, null);

                    return((T)addIn);
                }
            }
            //end direct connect code

            // Use Activator.CreateInstance instead of AppDomain.CreateInstanceAndUnwrap
            // because Activator will do the appropriate security asserts in the
            // remote appdomain.
            Type t = typeof(ActivationWorker);

            Object[]     args      = new Object[] { pipeline };
            ObjectHandle objHandle = Activator.CreateInstance(domain, t.Assembly.FullName, t.FullName,
                                                              false, BindingFlags.Instance | BindingFlags.NonPublic, null,
                                                              args, null, null);
            ActivationWorker activationWorker = (ActivationWorker)objHandle.Unwrap();

            activationWorker.UsingHostAppDomain = usingHostAppDomain;

            System.AddIn.Contract.IContract addInContract = null;
            try
            {
                addInContract = activationWorker.Activate();
            }
            catch (Exception ex)
            {
                CheckForDuplicateAssemblyProblems(pipeline, ex);
                throw;
            }

            if (weOwn)
            {
                domain.SetData(ContractHandle.s_appDomainOwner, addInContract);
            }

            controller.ActivationWorker = activationWorker;
            T hav = AdaptToHost <T>(pipeline, addInContract);

            controller.AssociateWithHostAddinView(hav, addInContract);

            return(hav);
        }
        internal AddInControllerImpl(AddInEnvironment environment, bool unloadDomainOnExit, AddInToken token)
        {
            System.Diagnostics.Contracts.Contract.Requires(environment != null);
            System.Diagnostics.Contracts.Contract.Requires(token != null);

            _unloadDomainOnExit = unloadDomainOnExit;
            _token = token;

            _addInEnvironment = environment;
        }