Click here to Skip to main content
15,885,546 members
Articles / Desktop Programming / WPF

Catel - Part 5 of n: Building a WPF example application with Catel in 1 hour

Rate me:
Please Sign up or sign in to vote.
4.84/5 (21 votes)
28 Jan 2011CPOL27 min read 99.4K   3.5K   37  
This article explains how to write a real-world application using Catel.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions