public virtual T GetAdapter()

in src/TestFramework/Core/DefaultTestSite.cs [341:443]


        public virtual T GetAdapter<T>() where T : IAdapter
        {
            // Set default value for compiling.
            T adapter = default(T);
            Type adapterType = typeof(T);

            if (this.configData == null)
            {
                throw new InvalidOperationException("Configuration files is not present");
            }

            // Check if target type adapter is already created.
            if (adapters.ContainsKey(adapterType))
            {
                return (T)adapters[adapterType];
            }

            // Get target adapter type.
            AdapterConfig adapterConfig = this.configData.GetAdapterConfig(adapterType.Name);

            if (adapterConfig is InteractiveAdapterConfig)
            {
                adapter = InteractiveAdapterProxy.Wrap<T>(adapterType);
            }
            // Create proxy for PowerShell script type adapter
            else if (adapterConfig is PowerShellAdapterConfig)
            {
                string scriptDir = Path.Combine(ptfconfigDirectory, ((PowerShellAdapterConfig)adapterConfig).ScriptDir);
                adapter = PowerShellAdapterProxy.Wrap<T>(
                    scriptDir,
                    adapterType);
            }

            // Create proxy for Shell script type adapter
            else if (adapterConfig is ShellAdapterConfig)
            {
                string scriptDir = ((ShellAdapterConfig)adapterConfig).ScriptDir;
                adapter = ShellAdapterProxy.Wrap<T>(
                    scriptDir,
                    adapterType);
            }

            // Create instance for dot net type adapter.
            if (adapterConfig is ManagedAdapterConfig)
            {
                try
                {
                    string adapterTypeName = ((ManagedAdapterConfig)adapterConfig).AdapterType;
                    if (adapterType.IsGenericType)
                    {
                        IAdapter instance = TestToolHelpers.CreateInstanceFromTypeName(adapterTypeName) as IAdapter;
                        adapter = (T)instance;
                    }
                    else
                    {
                        Type adapterImplType = TestToolHelpers.ResolveTypeFromAssemblies(adapterTypeName, testAssemblyDirectory);
                        if (adapterImplType == null)
                            throw new InvalidOperationException(
                                String.Format("Can't find assembly \"{0}\"", adapterTypeName));

                        adapter = ManagedAdapterProxy.Wrap<T>(adapterImplType, adapterType);
                    }
                    // adapter is null if as operator fails due to an object can't be converted to IAdapter type
                    if (adapter == null)
                    {
                        throw new InvalidOperationException(
                            String.Format("Adapter {0} does not implement {1}",
                                          adapterTypeName, adapterType.FullName));
                    }
                }
                catch (InvalidOperationException ex)
                {
                    throw new InvalidOperationException(
                               String.Format("Adapter {0} instance creation failed. Reason:{1}",
                                             adapterType.FullName, ex.ToString()));
                }
                catch (FileLoadException e)
                {
                    throw new InvalidOperationException(
                        String.Format("The assembly of the adapter ({0}) could not be loaded.", adapterType.Name), e);
                }
                catch (FileNotFoundException e)
                {
                    throw new InvalidOperationException(
                        String.Format("The assembly of the adapter ({0}) could not be found.", adapterType.Name), e);
                }
                catch (ArgumentException e)
                {
                    throw new InvalidOperationException(
                        String.Format("The type of the adapter ({0}) could not be found.", adapterType.Name), e);
                }
            }

            if (adapter == null)
            {
                throw new InvalidOperationException(String.Format("Failed while creating the adapter: {0}", adapterType.Name));
            }

            adapters.Add(adapterType, adapter);
            adapter.Initialize(this);

            return adapter;
        }