52 lines
1.8 KiB
C#
52 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Specialized;
|
|
using System.Configuration;
|
|
using System.Configuration.Provider;
|
|
namespace SPSolutions.Configuration.Provider
|
|
{
|
|
public class ProvidersHelper
|
|
{
|
|
public static ProviderBase InstantiateProvider(ProviderSettings providerSettings, Type providerType)
|
|
{
|
|
ProviderBase providerBase = null;
|
|
try
|
|
{
|
|
string text = (providerSettings.Type == null) ? null : providerSettings.Type.Trim();
|
|
if (string.IsNullOrEmpty(text))
|
|
{
|
|
throw new ArgumentException("Provider_no_type_name");
|
|
}
|
|
Type type = ConfigUtil.GetType(text, "type", providerSettings, true, true);
|
|
if (!providerType.IsAssignableFrom(type))
|
|
{
|
|
throw new ArgumentException("Provider_must_implement_type");
|
|
}
|
|
providerBase = (ProviderBase)Activator.CreateInstance(type);
|
|
NameValueCollection parameters = providerSettings.Parameters;
|
|
NameValueCollection nameValueCollection = new NameValueCollection(parameters.Count, StringComparer.Ordinal);
|
|
foreach (string name in parameters)
|
|
{
|
|
nameValueCollection[name] = parameters[name];
|
|
}
|
|
providerBase.Initialize(providerSettings.Name, nameValueCollection);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
if (ex is ConfigurationException)
|
|
{
|
|
throw;
|
|
}
|
|
throw new ConfigurationErrorsException(ex.Message, providerSettings.ElementInformation.Properties["type"].Source, providerSettings.ElementInformation.Properties["type"].LineNumber);
|
|
}
|
|
return providerBase;
|
|
}
|
|
public static void InstantiateProviders(ProviderSettingsCollection configProviders, ProviderCollection providers, Type providerType)
|
|
{
|
|
foreach (ProviderSettings providerSettings in configProviders)
|
|
{
|
|
providers.Add(ProvidersHelper.InstantiateProvider(providerSettings, providerType));
|
|
}
|
|
}
|
|
}
|
|
}
|