Click here to Skip to main content
15,887,027 members
Articles / Programming Languages / C# 4.0

MEF Generics

Rate me:
Please Sign up or sign in to vote.
5.00/5 (8 votes)
2 Feb 2012CPOL8 min read 44.3K   957   32  
How we can harness the power of MEF and extend it by way of generics.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.InteropServices;
using System.IO;
using Mef.Extended.Tools.Collections;

namespace Mef.Extended.Tools.Reflection
{
    public class AssemblyResolver 
    {
        public AssemblyResolver(params Assembly[] assemblies) : this(() => assemblies) { }

        public AssemblyResolver(Func<Assembly[]> getList)
        {
            Assemblies = new AssemblyList(getList());
        }

        public AssemblyResolver(Func<string, bool> searchFilter = null)
        {
            Assemblies = new AssemblyList(GetAssemblies(searchFilter));
        }

        public AssemblyList Assemblies { get; set; }

        public static Assembly[] GetReferencedAssemblies()
        {
            return AppDomain.CurrentDomain.GetAssemblies();
        }

        public Assembly[] GetAssemblies([Optional]Func<string, bool> searchFilter)
        {
            var assemblies = Directory.GetFileSystemEntries(AppDomain.CurrentDomain.BaseDirectory, "*.dll", SearchOption.AllDirectories);
            var currentAssemblies = searchFilter == null ? assemblies : assemblies.Where(searchFilter).ToArray();
            return currentAssemblies.Select(Assembly.LoadFrom).Union(new[] { Assembly.GetExecutingAssembly() }).ToArray();
        }
    }
}

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 BBD Johannesburg
South Africa South Africa
Bsc (Hons) Business Information Systems.
MCTS: Web Applications Development with Microsoft .NET Framework 4
MCTS: Windows Communication Foundation Development with Microsoft .NET Framework 4
MCTS: Accessing Data with Microsoft .NET Framework 4
Microsoft Certified Professional Developer Certification.

Comments and Discussions