Exemplo n.º 1
0
        private static Type GetProviderDesignTimeServices(string provider, bool throwOnError)
        {
            Assembly providerAssembly;

            try
            {
                providerAssembly = Assembly.Load(new AssemblyName(provider));
            }
            catch (Exception ex)
            {
                if (!throwOnError)
                {
                    return(null);
                }

                throw new OperationException(CommandsStrings.CannotFindRuntimeProviderAssembly(provider), ex);
            }

            var providerServicesAttribute = providerAssembly.GetCustomAttribute <DesignTimeProviderServicesAttribute>();

            if (providerServicesAttribute == null)
            {
                if (!throwOnError)
                {
                    return(null);
                }

                throw new InvalidOperationException(
                          CommandsStrings.CannotFindDesignTimeProviderAssemblyAttribute(
                              nameof(DesignTimeProviderServicesAttribute),
                              provider));
            }

            try
            {
                return(Type.GetType(
                           providerServicesAttribute.FullyQualifiedTypeName,
                           throwOnError: true,
                           ignoreCase: false));
            }
            catch (Exception ex)
                when(ex is FileNotFoundException || ex is FileLoadException || ex is BadImageFormatException)
                {
                    if (!throwOnError)
                    {
                        return(null);
                    }

                    throw new OperationException(
                              CommandsStrings.CannotFindDesignTimeProviderAssembly(providerServicesAttribute.PackageName), ex);
                }
        }
        private static IDesignTimeMetadataProviderFactory GetProviderDesignTimeServicesBuilder(string provider)
        {
            Assembly providerAssembly;

            try
            {
                providerAssembly = Assembly.Load(new AssemblyName(provider));
            }
            catch (Exception ex)
            {
                throw new OperationException(CommandsStrings.CannotFindRuntimeProviderAssembly(provider), ex);
            }

            var providerServicesAttribute = providerAssembly.GetCustomAttribute <DesignTimeProviderServicesAttribute>();

            if (providerServicesAttribute == null)
            {
                throw new InvalidOperationException(
                          CommandsStrings.CannotFindDesignTimeProviderAssemblyAttribute(
                              nameof(DesignTimeProviderServicesAttribute),
                              provider));
            }

            var      providerServicesAssemblyName = providerServicesAttribute.AssemblyName;
            Assembly providerServicesAssembly;

            if (providerServicesAssemblyName != null)
            {
                try
                {
                    providerServicesAssembly = Assembly.Load(new AssemblyName(providerServicesAssemblyName));
                }
                catch (Exception ex)
                {
                    throw new OperationException(
                              CommandsStrings.CannotFindDesignTimeProviderAssembly(providerServicesAssemblyName), ex);
                }
            }
            else
            {
                providerServicesAssembly = providerAssembly;
            }

            var providerServicesType = providerServicesAssembly.GetType(
                providerServicesAttribute.TypeName,
                throwOnError: true,
                ignoreCase: false);

            return((IDesignTimeMetadataProviderFactory)Activator.CreateInstance(providerServicesType));
        }