using System; using System.Collections.Generic; using System.IO; using System.Reflection; namespace SocialMediaStream { /// <summary> /// Factory responsible to create all <see cref="ISocialMediaProvider"/> instances. /// </summary> public class SocialMediaProviderFactory : ISocialMediaProviderFactory { #region Variables private static readonly List<ISocialMediaProvider> _providers = new List<ISocialMediaProvider>(); #endregion /// <summary> /// Gets the <see cref="ISocialMediaProvider"/> instances in the current working directory. /// </summary> /// <returns><see cref="IEnumerable{ISocialMediaProvider}"/> containing all providers in the current working directory.</returns> public IEnumerable<ISocialMediaProvider> GetProviders() { lock (_providers) { if (_providers.Count > 0) { return _providers; } string[] files = Directory.GetFiles(Environment.CurrentDirectory, "*.dll"); foreach (string file in files) { try { Assembly assembly = Assembly.LoadFile(file); foreach (Type type in assembly.GetTypes()) { if (type.IsSubclassOf(typeof(SocialMediaProviderBase))) { _providers.Add((ISocialMediaProvider)Activator.CreateInstance(type)); } } } catch (Exception) { // Continue } } return _providers; } } } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)